package com.sjkj.base_lib.tree.view;

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

import androidx.lifecycle.ViewModelProvider;

import com.sjkj.base_lib.R;
import com.sjkj.base_lib.app.BaseApp;
import com.sjkj.base_lib.databinding.ActivitySelectDeptBinding;
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.SelectTreeAdapter;
import com.sjkj.base_lib.tree.bean.MailTreeListBean;
import com.sjkj.base_lib.tree.vm.TreeViewModel;
import com.sjkj.base_lib.view.BaseActivity;
import com.sjkj.base_lib.weight.LoadingDialog;
import com.sjkj.base_lib.weight.tree.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门选择树
 */
public class SelectTreeDeptActivity extends BaseActivity {
    private ActivitySelectDeptBinding binding;
    private TreeViewModel viewModel;
    private MailListRequest treeListRequest = new MailListRequest(BaseApp.getInstance());
    protected List<Node> deptNodeList = new ArrayList<Node>();
    private SelectTreeAdapter adapter;
    public static final int TREE_RESULT_CODE = 1234;

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

    @Override
    public void initBinding() {
        super.initBinding();
        binding = (ActivitySelectDeptBinding) 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");
        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;
//                }
//                Intent intent = new Intent();
//                intent.putExtra("selectId", viewModel.selectId.get());
//                intent.putExtra("selectName", viewModel.selectName.get());
//                setResult(TREE_RESULT_CODE, intent);
//                finish();
//            }
//        });
        adapter = new SelectTreeAdapter(binding.lvTree, SelectTreeDeptActivity.this, deptNodeList, 0, R.drawable.tree_ex, R.drawable.tree_ec);
        adapter.setDetailClickListener(new SelectTreeAdapter.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)));
                    }
                }
                adapter.addDataAll(deptNodeList, 1);
            }
        });
    }
}
