package com.wd.workoffice.ui.activity.bat.work;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.DepBean;
import com.wd.workoffice.bean.DeviceBean;
import com.wd.workoffice.bean.event.ModifyDepPersonEvent;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkDepManageAdapter;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

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

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 部门成员
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkDepPersonActivity extends WorkToolBarActivity {

    @BindView(R.id.rv_manger)
    RecyclerView rvManger;
    @BindView(R.id.rv_simple)
    RecyclerView rvSimple;
    @BindView(R.id.tv_manage)
    TextView tvManage;
    @BindView(R.id.tv_simple)
    TextView tvSimple;
    private List<String> manageList;
    private List<String> simpleList;
    private WorkDepManageAdapter manageAdapter;
    private WorkDepManageAdapter simpleAdapter;
    private List<DepBean.DeptUserListBean> simple;
    private List<DepBean.DeptUserListBean> manage;
    private String depId;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvSimple.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvManger.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        depId = getIntent().getStringExtra("depId");
        simple = JSON.parseArray(getIntent().getStringExtra("simple"), DepBean.DeptUserListBean.class);
        manage = JSON.parseArray(getIntent().getStringExtra("manage"), DepBean.DeptUserListBean.class);
        manageList = new ArrayList<>();
        simpleList = new ArrayList<>();
        for (DepBean.DeptUserListBean deptUserListBean : simple) {
            simpleList.add(deptUserListBean.getNickName());
        }
        for (DepBean.DeptUserListBean deptUserListBean : manage) {
            manageList.add(deptUserListBean.getNickName());
        }
        if (manageList.size() == 0) {
            tvManage.setVisibility(View.GONE);
        }
        if (simpleList.size() == 0) {
            tvSimple.setVisibility(View.GONE);
        }
        boolean isManager =false;
        if (TextUtils.equals(UserKeeper.getInstance().getUserDepId(), depId)&&TextUtils.equals( UserKeeper.getInstance().getUserDepRuler(),"1")){
            isManager=true;
        }
        manageAdapter = new WorkDepManageAdapter(R.layout.item_work_dep_person, manageList,0,isManager);
        simpleAdapter = new WorkDepManageAdapter(R.layout.item_work_dep_person, simpleList,1,isManager);
        manageAdapter.bindToRecyclerView(rvManger);
        simpleAdapter.bindToRecyclerView(rvSimple);
    }


    @Override
    protected void initEvent() {
        manageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (!WorkUtils.hasPermission(PagePermissionType.SYS_UPDATE.getPermission())) {
                    toast(R.string.permission);
                    return;
                }
                removeManager(i);
            }
        });
        simpleAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (!WorkUtils.hasPermission(PagePermissionType.SYS_UPDATE.getPermission())) {
                    toast(R.string.permission);
                    return;
                }
                addManager(i);
            }
        });
    }

    private void addManager(int position) {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("userId",simple.get(position).getId());
        RtfUtils.getRtf().addDeptRuler(simple.get(position).getId()+"",WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                toast("操作成功");
                DepBean.DeptUserListBean dataBean = simple.get(position);
                manage.add(dataBean);
                simple.remove(position);
                simpleList.clear();
                manageList.clear();
                for (DepBean.DeptUserListBean deptUserListBean : simple) {
                    simpleList.add(deptUserListBean.getNickName());
                }
                for (DepBean.DeptUserListBean deptUserListBean : manage) {
                    manageList.add(deptUserListBean.getNickName());
                }
                if (manageList.size() == 0) {
                    tvManage.setVisibility(View.GONE);
                }
                if (simpleList.size() == 0) {
                    tvSimple.setVisibility(View.GONE);
                }
                manageAdapter.notifyDataSetChanged();
                simpleAdapter.notifyDataSetChanged();
                EventBus.getDefault().post(new ModifyDepPersonEvent());
            }
        });
    }

    private void removeManager(int position) {
        RtfUtils.getRtf().deleteDeptRuler(manage.get(position).getId()+"",depId).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                toast("操作成功");
                DepBean.DeptUserListBean dataBean = manage.get(position);
                simple .add(dataBean);
                manage.remove(position);
                simpleList.clear();
                manageList.clear();
                for (DepBean.DeptUserListBean deptUserListBean : simple) {
                    simpleList.add(deptUserListBean.getNickName());
                }
                for (DepBean.DeptUserListBean deptUserListBean : manage) {
                    manageList.add(deptUserListBean.getNickName());
                }
                if (manageList.size() == 0) {
                    tvManage.setVisibility(View.GONE);
                }
                if (simpleList.size() == 0) {
                    tvSimple.setVisibility(View.GONE);
                }
                manageAdapter.notifyDataSetChanged();
                simpleAdapter.notifyDataSetChanged();
                EventBus.getDefault().post(new ModifyDepPersonEvent());
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_work_dep_person;
    }

}
