package com.wd.workoffice.util.ddmfilter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkApp;
import com.wd.workoffice.bean.event.AddDeviceEvent;
import com.wd.workoffice.bean.listener.ThreeMenuClickListener;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.FilterAdapter;
import com.wd.workoffice.util.WorkUtils;
import com.wd.workoffice.widget.DropDownMenu;

import org.greenrobot.eventbus.EventBus;

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

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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 ProFilterData {
    DropDownMenu menu;
    private List<View> viewList;
    private View mainView;
    private RecyclerView rvData;
    private SmartRefreshLayout srlRefresh;
    private List<String> statusList = Arrays.asList("全部状态", "正常", "冻结");
    //    private List<String> processList = Arrays.asList("全部流程", "外采购流程", "供应流程", "销售流程", "坩埚流程");
    private List<String> processList = new ArrayList<>();
    private List<JSONObject> processDataList = new ArrayList<>();
    private List<String> onSaleList = Arrays.asList("全部", "已上架", "未上架");
    ThreeMenuClickListener listener;

    String[] TABLIST = new String[]{"全部状态", "全部流程", "是否上架"};
    Context context;
    private FilterAdapter processAdapter;

    public ProFilterData(Context context, DropDownMenu menu, ThreeMenuClickListener listener) {
        this.context = context;
        this.menu = menu;
        this.listener = listener;
        initView();
        getData();
    }


    private void initView() {
        mainView = LayoutInflater.from(context).inflate(R.layout.view_pro, null);
        rvData = mainView.findViewById(R.id.rv_data);
        srlRefresh = mainView.findViewById(R.id.srl_refresh);
        rvData.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        viewList = new ArrayList<>();
        addTabView(statusList, 1);
        addProcessTabView(processList, 2);
        addTabView(onSaleList, 3);
        menu.setDropDownMenu(Arrays.asList(TABLIST), viewList, mainView);
    }

    private void addProcessTabView(List<String> dataList, int type) {
        View view = LayoutInflater.from(WorkApp.getApp()).inflate(R.layout.view_filter_pro, null);
        RecyclerView rvData = view.findViewById(R.id.rv_filter);
        rvData.setLayoutManager(new LinearLayoutManager(WorkApp.getApp(), RecyclerView.VERTICAL, false));
        processAdapter = new FilterAdapter(R.layout.item_filter, dataList);
        processAdapter.bindToRecyclerView(rvData);
        viewList.add(view);
        processAdapter.setOnItemClickListener((adapter, v, position) -> {
            processAdapter.setChoosePos(position);
            menu.setTabText(dataList.get(position));
            if (listener != null) {
                switch (type) {
                    case 1:
                        listener.clickOne(dataList.get(position));
                        break;
                    case 2:
                        listener.clickTwo(processDataList.get(position).getString("subKey"));
                        break;
                    case 3:
                        listener.clickThree(dataList.get(position));
                        break;
                }
            }
            menu.closeMenu();
            processAdapter.notifyDataSetChanged();
        });
    }

    private void addTabView(List<String> dataList, int type) {
        View view = LayoutInflater.from(WorkApp.getApp()).inflate(R.layout.view_filter_pro, null);
        RecyclerView rvData = view.findViewById(R.id.rv_filter);
        rvData.setLayoutManager(new LinearLayoutManager(WorkApp.getApp(), RecyclerView.VERTICAL, false));
        FilterAdapter filterAdapter = new FilterAdapter(R.layout.item_filter, dataList);
        filterAdapter.bindToRecyclerView(rvData);
        viewList.add(view);
        filterAdapter.setOnItemClickListener((adapter, v, position) -> {
            filterAdapter.setChoosePos(position);
            menu.setTabText(dataList.get(position));
            if (listener != null) {
                switch (type) {
                    case 1:
                        listener.clickOne(dataList.get(position));
                        break;
                    case 2:
                        listener.clickTwo(dataList.get(position));
                        break;
                    case 3:
                        listener.clickThree(dataList.get(position));
                        break;
                }
            }
            menu.closeMenu();
            filterAdapter.notifyDataSetChanged();
        });
    }

    private void getData() {
        RtfUtils.getRtf().productProcess().compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
            }

            @Override
            public void onSuccess(BaseBean data) {
                if (data.getCode() != 0) {
                    return;
                }
                JSONObject all = new JSONObject();
                all.put("name", "全部流程");
                all.put("subKey", "");
                processDataList.add(all);
                processDataList.addAll(JSON.parseArray(data.getData().toString(), JSONObject.class));
                for (JSONObject process : processDataList) {
                    processList.add(process.getString("name"));
                }
                processAdapter.notifyDataSetChanged();
            }
        });
    }

    public SmartRefreshLayout getSrlRefresh() {
        return srlRefresh;
    }

    public void setSrlRefresh(SmartRefreshLayout srlRefresh) {
        this.srlRefresh = srlRefresh;
    }

    public RecyclerView getRvData() {

        return rvData;
    }

    public void setRvData(RecyclerView rvData) {
        this.rvData = rvData;
    }
}
