package com.sjkj.base_lib.tree.view;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.sjkj.base_lib.R;
import com.sjkj.base_lib.app.BaseApp;
import com.sjkj.base_lib.camera.dh.bean.CameraBean;
import com.sjkj.base_lib.databinding.ActivitySelectMultipleDeptBinding;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.net.request.MailListRequest;
import com.sjkj.base_lib.tree.adapter.SelectTreeMultipleDeptAdapter;
import com.sjkj.base_lib.tree.bean.MailTreeListBean;
import com.sjkj.base_lib.tree.vm.TreeViewModel;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseActivity;
import com.sjkj.base_lib.weight.LoadingDialog;
import com.sjkj.base_lib.weight.tree.Node;

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

/**
 * 部门选择树 选择多个
 */
public class SelectTreeMultipleDeptActivity extends BaseActivity {
    private ActivitySelectMultipleDeptBinding binding;
    private TreeViewModel viewModel;
    private MailListRequest treeListRequest = new MailListRequest(BaseApp.getInstance());
    protected List<Node> deptNodeList = new ArrayList<Node>();
    private SelectTreeMultipleDeptAdapter adapter;
    public static final int TREE_RESULT_CODE = 1234789;
    private List<String> selectedIdList = new ArrayList<>();
    private List<String> selectedNameList = new ArrayList<>();
    private List<String> selectFromIdList;

    @Override
    protected int initLayout() {
        return R.layout.activity_select_multiple_dept;
    }

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

    @Override
    public void initView() {
        super.initView();
        String titleName = getIntent().getStringExtra("titleName");
        selectFromIdList = (List<String>) getIntent().getSerializableExtra("selectFromIdList");
        getTitleView().setText(TextUtils.isEmpty(titleName) ? "选择所属部门" : "选择" + titleName);
        getRightView().setText("确定");
        getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (TextUtils.isEmpty(viewModel.selectId.get())) {
//                    ToastUtils.show("请选择" + titleName);
//                    return;
//                }
                selectedIdList.clear();
                selectedNameList.clear();
                if (adapter.getAllNodes() != null && adapter.getAllNodes().size() > 0) {
                    for (int i = 0; i < adapter.getAllNodes().size(); i++) {
                        if (adapter.getAllNodes().get(i).isChecked()){
                            selectedIdList.add(adapter.getAllNodes().get(i).getId().toString());
                            selectedNameList.add(adapter.getAllNodes().get(i).getName());
                        }
                    }
                }

                Intent intent = new Intent();
                intent.putExtra("selectedIdList", (Serializable) selectedIdList);
                intent.putExtra("selectedNameList", (Serializable) selectedNameList);
                setResult(TREE_RESULT_CODE, intent);
                finish();
            }
        });
        adapter = new SelectTreeMultipleDeptAdapter(binding.lvTree, SelectTreeMultipleDeptActivity.this, deptNodeList, 0, R.drawable.tree_ex, R.drawable.tree_ec);
//        adapter.setDetailClickListener(new SelectTreeMultipleDeptAdapter.OnDetailClickListener() {
//            @Override
//            public void click(Node node, int position) {
//                viewModel.selectId.set(node.getId() + "");
//                viewModel.selectName.set(node.getName() + "");
//                Intent intent = new Intent();
//                intent.putExtra("selectId", viewModel.selectId.get());
//                intent.putExtra("selectName", viewModel.selectName.get());
//                setResult(TREE_RESULT_CODE, intent);
//                finish();
//            }
//        });
        binding.lvTree.setAdapter(adapter);
    }

    @Override
    public void loadData() {
        super.loadData();
        Params params = new Params();
        treeListRequest.setParams(params);
        treeListRequest.execute(new ProcessErrorSubscriber<List<MailTreeListBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                hideLoadingDialog(LoadingDialog.LOADING_FAILED);

            }

            @Override
            public void onNext(List<MailTreeListBean> beans) {
                super.onNext(beans);
                hideLoadingDialog(LoadingDialog.LOADING_SUCCESS);
                deptNodeList.clear();
                if (beans != null && beans.size() >= 0) {
                    for (int i = 0; i < beans.size(); i++) {
                        deptNodeList.add(new Node(beans.get(i).getDeptId(), beans.get(i).getParentId(), beans.get(i).getDeptName(), beans.get(i)));
                    }
                    if (selectFromIdList!=null&&selectFromIdList.size()>0&&deptNodeList!=null&&deptNodeList.size()>0){
                        for (int i = 0; i < deptNodeList.size(); i++) {
                            for (int j = 0; j < selectFromIdList.size(); j++) {
                                if (TextUtils.equals(deptNodeList.get(i).getId().toString(), selectFromIdList.get(j))) {
                                    deptNodeList.get(i).setChecked(true);
                                    continue;
                                }
                            }
                        }
                    }
                }
                adapter.addDataAll(deptNodeList, 1);
            }
        });
    }
}
