package com.wisewoods.eduandroid.ui;

import android.os.Bundle;
import android.widget.CompoundButton;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.google.gson.reflect.TypeToken;
import com.wisewoods.base.base.BaseActivity;
import com.wisewoods.base.bean.TreePeopleBean;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityMettingPeopleBinding;
import com.wisewoods.eduandroid.ui.adapter.PeopleAdapter;

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

import retrofit2.Response;

public class MettingPeopleActivity extends BaseActivity<ActivityMettingPeopleBinding>
        implements CompoundButton.OnCheckedChangeListener {

    private List<Long> ids;
    private PeopleAdapter mAdapter;
    private boolean isAll = true;

    @Override
    protected void initData(Bundle savedInstanceState) {
        String membersStr = getIntent().getStringExtra("ids");
        ids = GsonUtils.fromJson(membersStr, new TypeToken<List<Long>>() {
        }.getType());

        showBarBack(binding.bar.tvTitle, "选择院内参会人员", binding.bar.ivBack);
        binding.tvOk.setOnClickListener(v -> {
            selectMembers();
        });
        binding.srf.setOnRefreshListener(() -> {
            getData();
        });
        binding.cbAll.setOnCheckedChangeListener(this);

        binding.rvPeople.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PeopleAdapter();
        binding.rvPeople.setAdapter(mAdapter);
        getData();
    }

//    public void getSub(List<TreePeopleBean.DataBean> datas, TreePeopleBean.DataBean f, boolean isAdd) {
//        if (isAdd) {
//            TreePeopleBean.DataBean item = new TreePeopleBean.DataBean();
//            item.deptId = f.deptId;
//            item.userId = f.userId;
//            item.deptName = f.deptName;
//            datas.add(item);
//        }
//        if (f.children.size() > 0 && !f.isLastLevel()) {
//            for (TreePeopleBean.DataBean child : f.children) {
//                getSub(datas, child, true);
//            }
//        }
//    }


    protected void getData() {
        App.api.getSystemTreeUser()
                .enqueue(new JCallback<TreePeopleBean>() {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        binding.srf.setRefreshing(false);
                    }

                    @Override
                    public void onS(Response<TreePeopleBean> response, TreePeopleBean treePeopleBean) {
                        if (treePeopleBean.data.size() > 0) {
                            TreePeopleBean.DataBean dataBean = treePeopleBean.data.get(0);
                            List<TreePeopleBean.DataBean> datas = new ArrayList<>();
//                            getSub(datas, dataBean, false);
                            selectSub(dataBean.getChildNode());
                            binding.cbAll.setOnCheckedChangeListener(null);
                            binding.cbAll.setChecked(isAll);
                            binding.cbAll.setOnCheckedChangeListener(MettingPeopleActivity.this);
                            mAdapter.setList(dataBean.children);
                        }

                    }
                });


//        App.api.getSystemUser()
//                .enqueue(new JCallback<SysUserList>() {
//                    @Override
//                    public void onS(Response<SysUserList> response, SysUserList sysUserList) {
//                        List<SysUserList.DataBean> rows = sysUserList.data.rows;
//                        for (SysUserList.DataBean row : rows) {
//                            if (ids.contains(row.userId)) {
//                                row.isCheck = true;
//                            } else {
//                                isAll = false;
//                            }
//                        }
//                        binding.cbAll.setOnCheckedChangeListener(null);
//                        binding.cbAll.setChecked(isAll);
//                        binding.cbAll.setOnCheckedChangeListener(MettingPeopleActivity.this);
//                        mAdapter.setNewInstance(sysUserList.data.rows);
//                    }
//
//                    @Override
//                    public void onFinish() {
//                        super.onFinish();
//                        binding.srf.setRefreshing(false);
//                    }
//                });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        changeSub(mAdapter.getData(), isChecked);
//        for (SysUserList.DataBean data : mAdapter.getData()) {
//            data.isCheck = isChecked;
//        }
        isAll = isChecked;
        mAdapter.notifyDataSetChanged();
    }

    private void selectMembers() {
        List<TreePeopleBean.DataBean> mList = new ArrayList<>();
//        for (SysUserList.DataBean data : mAdapter.getData()) {
//            if (data.isCheck) {
//                mList.add(data);
//            }
//        }
        getSub(mList, mAdapter.getData());
        getIntent().putExtra("members", GsonUtils.toJson(mList));
        setResult(CreateMettingActivity.CODE_RESULT_MEMBERS, getIntent());
        finish();
    }

    public void selectSub(List<BaseNode> datas) {
        if (datas == null) {
            return;
        }
        for (BaseNode data : datas) {
            TreePeopleBean.DataBean node = (TreePeopleBean.DataBean) data;
            if (node.isLastLevel()) {
                if (ids.contains(node.userId)) {
                    node.isCheck = true;
                } else {
                    isAll = false;
                }
            } else {
                if (!node.isExpanded()) {
                    selectSub(node.getChildNode());
                }
            }
        }
    }

    public void getSub(List<TreePeopleBean.DataBean> mList, List<BaseNode> datas) {
        if (datas == null) {
            return;
        }
        for (BaseNode data : datas) {
            TreePeopleBean.DataBean node = (TreePeopleBean.DataBean) data;
            if (node.isLastLevel() && node.isCheck) {
                mList.add(node);
            } else {
                if (!node.isExpanded()) {
                    getSub(mList, node.getChildNode());
                }
            }
        }
    }

    public void changeSub(List<BaseNode> datas, boolean isChecked) {
        if (datas == null) {
            return;
        }
        for (BaseNode data : datas) {
            TreePeopleBean.DataBean node = (TreePeopleBean.DataBean) data;
            if (node.isLastLevel()) {
                node.isCheck = isChecked;
            } else {
                if (!node.isExpanded()) {
                    changeSub(node.getChildNode(), isChecked);
                }
            }
        }
    }


}
