package com.sjkj.appthreefloor_tsgz.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.address.AddressSelector;
import com.sjkj.appthreefloor_tsgz.address.CityBean;
import com.sjkj.appthreefloor_tsgz.address.CityInterface;
import com.sjkj.appthreefloor_tsgz.address.OnFinishClickListener;
import com.sjkj.appthreefloor_tsgz.address.OnItemClickListener;
import com.sjkj.appthreefloor_tsgz.app.App;
import com.sjkj.appthreefloor_tsgz.bean.AppEventBean;
import com.sjkj.appthreefloor_tsgz.bean.AppEventThreeBean;
import com.sjkj.appthreefloor_tsgz.net.AppEventThreeRequest;
import com.sjkj.appthreefloor_tsgz.net.AppEventTypeRequest;
import com.sjkj.base_lib.app.AppManager;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.utils.CommonUtils;
import com.sjkj.base_lib.utils.ToastUtils;

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

public class EventDepartmentTools {
    private static ArrayList<CityBean> cities1 = new ArrayList<>();
    private static ArrayList<CityBean> cities2 = new ArrayList<>();
    private static ArrayList<CityBean> cities3 = new ArrayList<>();
    private static AppEventTypeRequest appEventTypeRequest = new AppEventTypeRequest(App.getInstance());
    private static AppEventThreeRequest appEventThreeRequest = new AppEventThreeRequest(App.getInstance());
    private static String strONE = "";
    private static String strTWO = "";
    private static String strThree = "";

    private static String oneID = "";
    private static String threeID = "";
    private static String twoID = "";
    public static OnFinishClickListener onFinishClickListener;

    /**
     * 设置列表的点击事件回调接口
     */
    public static void setOnFinishClickListener(OnFinishClickListener onFinishClickListener) {
        EventDepartmentTools.onFinishClickListener = onFinishClickListener;
    }

    public static void GetEventFL(Context context) {
        if (SoftKeyboardUtils.getKeyBoardIsShow(context)) {
            SoftKeyboardUtils.hideSoftKeyboard(AppManager.getAppManager().currentActivity());
        }
        cities1.clear();
        Params params = new Params();
        params.put("parentId", "0");
        appEventTypeRequest.setParams(params);
        appEventTypeRequest.execute(new ProcessErrorSubscriber<List<AppEventBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(List<AppEventBean> list) {
                super.onNext(list);

                CityBean bean = null;
                for (int i = 0; i < list.size(); i++) {
                    bean = new CityBean();
                    bean.setId(list.get(i).getId());
                    bean.setName(list.get(i).getEventTypeName());
                    cities1.add(bean);

                }
                showPop(context, cities1);
            }
        });


    }

    private static void showPop(Context context, ArrayList<CityBean> cities1) {

        //加载弹出框的布局
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_address, null);
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        ImageView imageView = contentView.findViewById(R.id.iv_back);
        TextView tv_title = contentView.findViewById(R.id.tv_title);


        PopWindowUtil.getInstance().makePopupWindow(context, contentView).showLocationWithAnimation(context, contentView);
        AddressSelector addressSelector = (AddressSelector) contentView.findViewById(R.id.address);
        if (AppSystemUtils.isGhb()) {
            addressSelector.setListTextSize(18);
            addressSelector.setTabTextSize(20);
            tv_title.setTextSize(20);
            tv_title.getPaint().setFakeBoldText(true);
            RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
            params.width= CommonUtils.dp2px(40);
            params.height= CommonUtils.dp2px(40);
            params.topMargin=CommonUtils.dp2px(10);
            imageView.setLayoutParams(params);

        }
        addressSelector.setTabAmount(2);
        addressSelector.setCities(cities1);
        // addressSelector.setGrayLineColor(context.getResources().getColor(R.color.color_main));
        addressSelector.setListTextSelectedColor(context.getResources().getColor(R.color.color_blue));
        if (cities1.size() > 0) {
            addressSelector.setBooleData(true);
        } else {
            addressSelector.setBooleData(false);
        }

        tv_title.setText("请选择事件类型");


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PopWindowUtil.mPopupWindow.dismiss();
            }
        });
        addressSelector.setOnTabSelectedListener(new AddressSelector.OnTabSelectedListener() {
            @Override
            public void onTabSelected(AddressSelector addressSelector, AddressSelector.Tab tab) {
                switch (tab.getIndex()) {

                    case 0:
                        if (cities1.size() > 0) {
                            addressSelector.setBooleData(true);
                        }
                        addressSelector.setCities(cities1);
                        break;
                    case 1:
                        if (cities2.size() > 0) {
                            addressSelector.setBooleData(true);
                        }
                        addressSelector.setCities(cities2);

                        break;
//                    case 2:
//                        if (cities3.size() > 0) {
//                            addressSelector.setBooleData(true);
//                        }
//                        addressSelector.setCities(cities3);
//
//                        break;


                }
            }

            @Override
            public void onTabReselected(AddressSelector addressSelector, AddressSelector.Tab tab) {

            }
        });


        addressSelector.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void itemClick(AddressSelector addressSelector, CityInterface city, int tabPosition, int position) {


                switch (tabPosition) {

                    case 0:
                        //0代表小区已经选择完毕，调用接口  显示楼栋数据
                        cities2.clear();
                        GetTwo(addressSelector, city, position);
                        strONE = cities1.get(position).getCityName();
                        oneID = cities1.get(position).getCityId();
                        break;
                    case 1:

                        cities3.clear();
//                        GetThree(addressSelector, city, position);
                        if (cities2.size() > 0) {
                            strTWO = cities2.get(position).getCityName();
                            twoID = cities2.get(position).getCityId();
                        }
                        if (onFinishClickListener != null) {
                            onFinishClickListener.FinishClick(strONE, oneID, strTWO, twoID, "", "", "", "");
                            PopWindowUtil.mPopupWindow.dismiss();
                        }
                        break;
//                    case 2:
//                        if (cities3.size()>0) {
//                            strThree = cities3.get(position).getCityName();
//                            threeID = cities3.get(position).getCityId();
//                        }
//
//                           if (onFinishClickListener != null) {
//                                onFinishClickListener.FinishClick(strONE, oneID, strTWO, twoID, strThree, threeID, "", "");
//                              PopWindowUtil.mPopupWindow.dismiss();
//                            }
//                        break;

                }
            }
        });
    }


    private static void GetThree(AddressSelector addressSelector, CityInterface city, int position) {
        Params params = new Params();
        params.put("eventTypeValue", city.getCityId());
        appEventThreeRequest.setParams(params);
        appEventThreeRequest.execute(new ProcessErrorSubscriber<List<AppEventThreeBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(List<AppEventThreeBean> list) {
                super.onNext(list);
                if (list.size() > 0) {
                    CityBean bean = null;
                    for (int i = 0; i < list.size(); i++) {
                        bean = new CityBean();
                        bean.setId(list.get(i).getId());
                        bean.setName(list.get(i).getDeptName());
                        cities3.add(bean);
                        addressSelector.setCities(cities3);
                    }
                    addressSelector.setBooleData(true);
                } else {
                    ToastUtils.show("暂无分类信息");
                    addressSelector.setCities(cities3);
                    addressSelector.setBooleData(false);
                }
            }
        });

    }

    private static void GetTwo(AddressSelector addressSelector, CityInterface city, int position) {

        Params params = new Params();
        params.put("parentId", city.getCityId());
        appEventTypeRequest.setParams(params);
        appEventTypeRequest.execute(new ProcessErrorSubscriber<List<AppEventBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(List<AppEventBean> list) {
                super.onNext(list);
                if (list.size() > 0) {
                    CityBean bean = null;
                    for (int i = 0; i < list.size(); i++) {
                        bean = new CityBean();
                        bean.setId(list.get(i).getId());
                        bean.setName(list.get(i).getEventTypeName());
                        cities2.add(bean);
                        addressSelector.setCities(cities2);
                    }
                    addressSelector.setBooleData(true);
                } else {
                    ToastUtils.show("暂无分类信息");
                    addressSelector.setCities(cities2);
                    addressSelector.setBooleData(false);
                }
            }
        });


    }


}
