package com.unis.sxzcl.ui.person;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.unis.model.bean.person.PersonBean;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentPersonListBinding;
import com.unis.sxzcl.widget.BottomMultipleListDialog;
import com.unis.sxzcl.widget.MultipleBean;
import com.xyxsbj.mylibrary.tool.ETTool;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 包名: com.unis.sxzcl.ui.person
 * 类名: PersonListFragment
 * 作用描述: 人员列表
 * 创建人: mQ
 * 创建时间: 2020/11/30 19:39
 * 更新人:
 * 更新时间: 2020/11/30 19:39
 * 更新说明:
 * 版本号: 1.0
 */
public class PersonListFragment extends BaseListFragment<FragmentPersonListBinding> implements OnItemClickListener {

    private PersonListAdapter mAdapter;
    private String mContent;
    private BottomMultipleListDialog mMultipleListDialog;
    private boolean mShowTitle;

    public static PersonListFragment newFragmentNoTitle() {
        Bundle bundle = new Bundle();
        bundle.putBoolean("showTitle", false);
        PersonListFragment fragment = new PersonListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onRefreshData() {
        getData(true);
    }

    @Override
    public void onMoreData() {
        super.onMoreData();
        getData(false);
    }

    @Override
    protected FragmentPersonListBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentPersonListBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        Bundle bundle = getArguments();
        mShowTitle = bundle.getBoolean("showTitle", false);
        if (mShowTitle) {
            initTitle();
        } else {
            mBinding.layoutTitle.setVisibility(View.GONE);
        }

        mAdapter = new PersonListAdapter();
        mAdapter.setOnItemClickListener(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

    private void initTitle() {
        mBinding.bar.setTitle("人员列表")
                .setLeftClickListener(v -> finish());

        mBinding.etSearch.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) {
                mBinding.ivClear.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        mBinding.btnSearch.setOnClickListener(v -> {
            String content = ETTool.getData(mBinding.etSearch);
            ETTool.closeSoftKeybord(mBinding.etSearch);
            mContent = content;
            onRefresh();
        });
        mBinding.ivClear.setVisibility(View.GONE);
        mBinding.ivClear.setOnClickListener(v -> {
            mBinding.etSearch.setText("");
            mContent = "";
            onRefresh();
        });
        mBinding.tvCity.setOnClickListener(v -> {
            showCity();
        });
    }

    @Override
    protected void onFirstShow() {
        onRefresh();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PersonBean bean = mAdapter.getData().get(position);
        Bundle bundle = new Bundle();
        bundle.putString("data", getGson().toJson(bean));
        navigation(R.id.personHomeFragment, bundle);
    }

    private void getData(boolean refresh) {
        new Handler().postDelayed(() -> {
            if (refresh) {
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
            }
//            mAdapter.addData(new PersonBean("陈晓军", "", "主任"));
//            mAdapter.addData(new PersonBean("Cherry", "", "仓储科科长"));
//            mAdapter.addData(new PersonBean("常道", "", "保管员"));
//            mAdapter.addData(new PersonBean("大鹏", "", "保管员"));
//            mAdapter.addData(new PersonBean("大大熊", "", "保管员"));
            loadEnd();
        }, 500);
    }

    private void showCity() {
        if (mMultipleListDialog == null) {
            mMultipleListDialog = new BottomMultipleListDialog(getContext())
                    .setTitle("")
                    .hideThreeList()
                    .hideRightEnter()
                    .setListOne(Arrays.asList(
                            new MultipleBean("全部"),
                            new MultipleBean("太原市"),
                            new MultipleBean("长治市"),
                            new MultipleBean("大同市"),
                            new MultipleBean("阳泉市"),
                            new MultipleBean("晋中市"),
                            new MultipleBean("朔州市"),
                            new MultipleBean("临汾市"),
                            new MultipleBean("忻州市"),
                            new MultipleBean("吕梁市"),
                            new MultipleBean("运城市")
                    ))
                    .setListTwo(new ArrayList<>())
                    .setOneCallback((v, bean) -> {
                        //选项一点击事件
                        if (bean.getName().contains("全部")) {
                            mBinding.tvCity.setText("全公司");
                            mMultipleListDialog.setListTwo(new ArrayList<>());
                            return true;
                        }
                        mMultipleListDialog.setListTwo(Arrays.asList(
                                new MultipleBean("全部"),
                                new MultipleBean("库区1"),
                                new MultipleBean("库区2"),
                                new MultipleBean("库区3")
                        ));
                        return false;
                    })
                    .setTwoCallback((v, b1, b2) -> {
                        //选项二点击事件
                        if (b2.getName().contains("全部")) {
                            mBinding.tvCity.setText(b1.getName() + " 全部库区");
                        } else {
                            mBinding.tvCity.setText(b2.getName());
                        }
                        return true;
                    });
        }
        mMultipleListDialog.show();
    }


}
