package com.zztrapp.zztrapp.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionSelectedListener;
import com.github.gzuliyujiang.wheelpicker.widget.OptionWheelLayout;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.bean.ChoosePopItemBean;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.IndexBean;
import com.zztrapp.zztrapp.databinding.FragmentIndexBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.index.activity.BfjListActivity;
import com.zztrapp.zztrapp.ui.index.activity.HeiCouShuiTiActivity;
import com.zztrapp.zztrapp.ui.index.activity.JcjListActivity;
import com.zztrapp.zztrapp.ui.index.activity.WrdkListActivity;
import com.zztrapp.zztrapp.ui.index.activity.WsclssListActivity;
import com.zztrapp.zztrapp.ui.index.activity.ZdjgdwListActivity;
import com.zztrapp.zztrapp.ui.index.adapter.IndexAdapter;
import com.zztrapp.zztrapp.utilcode.klogs.KLog;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;
import com.zztrapp.zztrapp.widget.MyOptionPicker;

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

/**
 * Describe IndexFragment
 * Created by Bby on 2022/6/22
 * Email bby0856@163.com
 */

public class IndexFragment extends Fragment {

    private FragmentIndexBinding binding;
    private IndexAdapter adapter;
    private boolean isShowDialog = true;
    private String countyCode = "";
    private List<IndexBean> list;
    private List<ChoosePopItemBean> xzqxList;
    private int selectOption = 0;//地址选择
    private MyOptionPicker picker;

    public IndexFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentIndexBinding.inflate(inflater, container, false);
        init();
        onClick();
        return binding.getRoot();
    }

    private void init() {
        KLog.e("onCreateView---------------");
        xzqxList = new ArrayList<>();
        picker = new MyOptionPicker(getActivity());
        selectOption = 0;
        countyCode="";
        list = new ArrayList<>();
        binding.refresh.setRefreshHeader(new ClassicsHeader(getActivity()).setAccentColor(getResources().getColor(R.color.white)));
        binding.refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                isShowDialog = false;
                binding.addressHint.setText(getString(R.string.all_regions));
                loadIndexList();
                refreshlayout.finishRefresh();
            }
        });

        binding.rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new IndexAdapter(getContext());
        binding.rv.setAdapter(adapter);
        loadIndexList();
    }

    private void onClick() {
        binding.address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.addressIcon.setRotation(180);
                picker.setData(xzqxList);
                if (xzqxList.size() > 0) {
                    picker.setDefaultPosition(selectOption);
                }
                picker.setOnOptionPickedListener(new OnOptionPickedListener() {
                    @Override
                    public void onOptionPicked(int position, Object item) {
                        selectOption = position;
                        isShowDialog = true;
                        countyCode = xzqxList.get(position).getCode();
                        binding.addressHint.setText(xzqxList.get(position).getName());
                        loadIndexList();
                    }
                });
                OptionWheelLayout wheelLayout = picker.getWheelLayout();
//                wheelLayout.setIndicatorEnabled(false);
                wheelLayout.setSelectedTextColor(getResources().getColor(R.color.color2C86FF));
                picker.getWheelLayout().setOnOptionSelectedListener(new OnOptionSelectedListener() {
                    @Override
                    public void onOptionSelected(int position, Object item) {

                    }
                });
                picker.show();


//                AddressPicker picker = new AddressPicker(getActivity());
//                if (!binding.addressHint.getText().equals("全部地区")) {
//                    picker.setDefaultValue(provinceName, cityName, countyName);
//                }
//                picker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY);
//                picker.setOnAddressPickedListener(new OnAddressPickedListener() {
//                    @Override
//                    public void onAddressPicked(ProvinceEntity province, CityEntity city, CountyEntity county) {
//                        provinceName = province.getName();
//                        cityName = city.getName();
//                        countyName = county.getName();
//                        isShowDialog = true;
//                        countyCode = county.getCode();
//                        binding.addressHint.setText(county.getName());
//                        loadIndexList();
//                    }
//                });
//                picker.setOnDismissListener(new DialogInterface.OnDismissListener() {
//                    @Override
//                    public void onDismiss(DialogInterface dialogInterface) {
//                        binding.addressIcon.setRotation(0);
//                    }
//                });
//                picker.show();
            }
        });
        adapter.setOnItemClickListener(new IndexAdapter.OnItemClickListener() {
            @Override
            public void onLookDetailClick(int type, String name) {
                switch (type) {
                    case 1://土壤环境-重点监管单位
                        if (name.equals("重点监管单位")) {
                            Intent intent1 = new Intent(getActivity(), ZdjgdwListActivity.class);
                            intent1.putExtra("countyCode", countyCode);
                            startActivity(intent1);
                        }
                        if (name.equals("土壤污染地块")) {
                            Intent intent1 = new Intent(getActivity(), WrdkListActivity.class);
                            intent1.putExtra("countyCode", countyCode);
                            startActivity(intent1);
                        }
                        break;
                    case 2://地下水环境-监测井
                        if (name.equals("监测井")) {
                            Intent intent2 = new Intent(getActivity(), JcjListActivity.class);
                            intent2.putExtra("countyCode", countyCode);
                            startActivity(intent2);
                        }
                        if (name.equals("报废井")) {
                            Intent intent2 = new Intent(getActivity(), BfjListActivity.class);
                            intent2.putExtra("countyCode", countyCode);
                            startActivity(intent2);
                        }
                        if (name.equals("双源调查")) {

                        }
                        break;
                    case 3://农业农村-黑臭水体
                        if (name.equals("黑臭水体")) {
                            Intent intent31 = new Intent(getActivity(), HeiCouShuiTiActivity.class);
                            intent31.putExtra("countyCode", countyCode);
                            startActivity(intent31);
                        }
                        if (name.equals("污水处理设施")) {
                            Intent intent32 = new Intent(getActivity(), WsclssListActivity.class);
                            intent32.putExtra("countyCode", countyCode);
                            startActivity(intent32);
                        }
                        break;
                }

            }
        });
    }


    //列表
    private void loadIndexList() {
        ISubscriberOnNextListener<HttpResult<List<IndexBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<IndexBean>>>() {
            @Override
            public void onNext(HttpResult<List<IndexBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    list = httpResult.getResult();
                    adapter.setData(list);
                    if (xzqxList.size() <= 0) {
                        loadXzqhList();
                    }
                } else {
                    list.clear();
                    adapter.setData(list);
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<IndexBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), isShowDialog);
        RxApiManager.get().add("indexList", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).indexList(httpResultProgressSubscriber, countyCode);
    }

    //行政区划列表
    private void loadXzqhList() {
        ISubscriberOnNextListener<HttpResult<List<ChoosePopItemBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<ChoosePopItemBean>>>() {
            @Override
            public void onNext(HttpResult<List<ChoosePopItemBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    xzqxList.clear();
                    ChoosePopItemBean choosePopItemBean = new ChoosePopItemBean();
                    choosePopItemBean.setName("全部地区");
                    choosePopItemBean.setCode("");
                    xzqxList.add(choosePopItemBean);
                    xzqxList.addAll(httpResult.getResult());
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<ChoosePopItemBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), true);
        RxApiManager.get().add("xzbmAreaList", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).xzbmAreaList(httpResultProgressSubscriber);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("indexList");
        RxApiManager.get().cancel("xzbmAreaList");
    }

}