package com.sjkj.base_lib.camera.dh.view;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sjkj.base_lib.R;
import com.sjkj.base_lib.app.BaseApp;
import com.sjkj.base_lib.camera.dh.adapter.DeviceAdapter;
import com.sjkj.base_lib.camera.dh.bean.CameraBean;
import com.sjkj.base_lib.camera.dh.vm.CameraViewModel;
import com.sjkj.base_lib.databinding.ActivityDeviceListBinding;
import com.sjkj.base_lib.divider.DividerItem;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.net.request.DeviceListRequest;
import com.sjkj.base_lib.utils.AppTools;
import com.sjkj.base_lib.utils.CommonUtils;
import com.sjkj.base_lib.view.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备选择
 */
public class DeviceListActivity extends BaseActivity {

    public static final String TAG = DeviceListActivity.class.getSimpleName();
    public static final int CODE_RESULT = 11013;
    private DeviceAdapter adapter;
    private List<CameraBean> list = new ArrayList<>();
    private int currentPage = 1;
    private ActivityDeviceListBinding binding;
    private CameraViewModel viewModel;
    private DeviceListRequest deviceListRequest = new DeviceListRequest(BaseApp.getInstance());
    private List<CameraBean> selectedList = new ArrayList<>();
    private List<CameraBean> selectedFromList;

    @Override
    public int initLayout() {
        return R.layout.activity_device_list;
    }

    @Override
    public void initBinding() {
        binding = (ActivityDeviceListBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(DeviceListActivity.this, new ViewModelProvider.NewInstanceFactory()).get(CameraViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }

    @Override
    public void initView() {
        getTitleView().setText("设备选择");
        getRightView().setText("确定");
        if (AppTools.isGhb()) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) binding.inputEt.getLayoutParams();
            params.height = CommonUtils.dp2px(50);
            binding.inputEt.setTextSize(18);
            binding.inputEt.getPaint().setFakeBoldText(true);
            binding.inputEt.setLayoutParams(params);
        }
        selectedFromList = (List<CameraBean>) getIntent().getSerializableExtra("selectDeviceList");
        if (selectedFromList != null && selectedFromList.size() > 0) {
            selectedList.addAll(selectedFromList);
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        binding.mainRecyclerView.setLayoutManager(linearLayoutManager);
        binding.mainRecyclerView.addItemDecoration(new DividerItem(this));

        adapter = new DeviceAdapter(this);
        adapter.setOnDeviceSelectListener(new DeviceAdapter.OnDeviceSelectListener() {
            @Override
            public void onSelectCheckListener(CameraBean bean) {//选中监听
                if (bean != null && selectedList != null) {
                    selectedList.add(bean);
                }
            }

            @Override
            public void onSelectUnCheckListener(CameraBean bean) {//取消监听
                if (bean != null && selectedList != null && selectedList.size() > 0) {
                    for (int i = 0; i < selectedList.size(); i++) {
                        if (TextUtils.equals(bean.getId(), selectedList.get(i).getId())) {
                            selectedList.remove(selectedList.get(i));
                        }
                    }
                }
            }
        });
        binding.mainRecyclerView.setAdapter(adapter);

        binding.smartRefreshLayout.setEnableRefresh(false);
        binding.smartRefreshLayout.setEnableLoadMore(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void initListener() {
        getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("selectDeviceList", (Serializable) selectedList);
                setResult(DeviceListActivity.CODE_RESULT, intent);
                finish();
            }
        });
        binding.inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                adapter.fillter(s.toString());
            }
        });
//        binding.upIv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DialogUtils.showCustomDelList(DeviceListActivity.this, "选择列表", selectedList, new DeviceDelRecycleViewAdapter.OnDelClickListener() {
//                    @Override
//                    public void onDelListener(CameraBean bean) {
//                        if (bean != null && selectedList != null && selectedList.size() > 0) {
//                            for (int i = 0; i < selectedList.size(); i++) {
//                                if (TextUtils.equals(selectedList.get(i).getId(), bean.getId())) {
//                                    selectedList.remove(i);
//                                }
//                            }
//                        }
//                    }
//                });
//            }
//        });

//        binding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshLayout) {
//                requestListRefresh();
//            }
//        });
//        binding.smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(RefreshLayout refreshLayout) {
//                requestListLoadMore();
//            }
//        });
    }

    @Override
    public void loadData() {
        requestListRefresh();
    }

    private void requestListRefresh() {
//        currentPage = 1;
        Params params = new Params();
//        params.put("deptId", AppTools.getDeptId());
        params.put("cameraName", viewModel.keyword.get());
//        params.put("pageNum", currentPage);
//        params.put("pageSize", 20);
        deviceListRequest.setParams(params);
        deviceListRequest.execute(new ProcessErrorSubscriber<List<CameraBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
//                finishRefreshOrLoadMore(binding.smartRefreshLayout);
            }

            @Override
            public void onNext(List<CameraBean> beanList) {
                super.onNext(beanList);
                list.clear();
                if (beanList != null && beanList.size() > 0) {
                    list.addAll(beanList);
                }
                if (selectedList!=null&&selectedList.size()>0&&list!=null&&list.size()>0){
                    for (int i = 0; i < list.size(); i++) {
                        for (int j = 0; j < selectedList.size(); j++) {
                            if (TextUtils.equals(list.get(i).getId(), selectedList.get(j).getId())) {
                                list.get(i).setCheck(true);
                                continue;
                            }
                        }
                    }
                }

                adapter.refreshList(list);
//                finishRefreshOrLoadMore(binding.smartRefreshLayout);
            }
        });
    }
//
//    private void requestListLoadMore() {
//        Params params = new Params();
//        params.put("cameraName", viewModel.keyword.get());
//        params.put("pageSize", 20);
//        params.put("deptId", AppTools.getDeptId());
//        params.put("pageNum", currentPage + 1);
//        cameraListRequest.setParams(params);
//        cameraListRequest.execute(new ProcessErrorSubscriber<List<CameraBean>>() {
//            @Override
//            public void onError(Throwable e) {
//                super.onError(e);
//                finishRefreshOrLoadMore(binding.smartRefreshLayout);
//            }
//
//            @Override
//            public void onNext(List<CameraBean> beanList) {
//                super.onNext(beanList);
//                if (beanList != null && beanList.size() > 0) {
//                    currentPage = currentPage + 1;
//                    list.addAll(beanList);
//                    adapter.refreshList(list);
//                }
//                finishRefreshOrLoadMore(binding.smartRefreshLayout);
//            }
//        });
//    }
}
