package com.rdiot.yx485.home;

import static com.rdiot.yx485.home.HomeCreateActivity.convertToPickerData;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.rdiot.yx485.base.BaseFragment;
import com.rdiot.yx485.bean.DefHomeRoomBean;
import com.rdiot.yx485.databinding.FragmentHomeAddressBinding;
import com.rdiot.yx485.util.LocalCache;

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

public class HomeAddressFragment extends BaseFragment {
    private final List<String> provinces = new ArrayList<>();
    private final List<List<String>> cities = new ArrayList<>();
    private final List<List<List<String>>> counties = new ArrayList<>();
    private String p, c, d, address;
    private FragmentHomeAddressBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentHomeAddressBinding.inflate(inflater, container, false);
        initView();
        initData();
        return mBinding.getRoot();
    }

    private void initView() {
        if (getActivity() instanceof HomeCreateActivity) {
            p = ((HomeCreateActivity) getActivity()).province;
            c = ((HomeCreateActivity) getActivity()).city;
            d = ((HomeCreateActivity) getActivity()).district;
            address = ((HomeCreateActivity) getActivity()).address;
        }
        mBinding.tvZone.setText(String.format("%s%s%s", p, c, d));
        mBinding.tvZone.setOnClickListener(v -> showSelectAddressDialog());
        mBinding.editAddress.setText(address);
        mBinding.btnConfirm.setOnClickListener(v -> {
            if (TextUtils.isEmpty(p) || TextUtils.isEmpty(c) || TextUtils.isEmpty(d)) {
                showMessage("请选择所在区域");
                return;
            }

            String address = mBinding.editAddress.getText().toString();
//            if (TextUtils.isEmpty(address)) {
//                showMessage(mBinding.editAddress.getHint().toString());
//                return;
//            }

            if (getActivity() instanceof HomeCreateActivity) {
                ((HomeCreateActivity) getActivity()).setAddress(p, c, d, address);
            }
        });
    }

    private void initData() {
        List<DefHomeRoomBean.DataBean.CityInfo> list = LocalCache.getCityList();
        if (list == null || list.isEmpty()) {
            return;
        }

        convertToPickerData(list, provinces, cities, counties);
    }

    private void showSelectAddressDialog() {
        if (provinces.isEmpty() || cities.isEmpty() || counties.isEmpty()) {
            return;
        }

        OptionsPickerView pvOptions = new OptionsPickerBuilder(requireContext(), (options1, option2, options3, v) -> {
            //返回的分别是三个级别的选中位置
            p = provinces.get(options1);
            c = cities.get(options1).get(option2);
            d = counties.get(options1).get(option2).get(options3);
            mBinding.tvZone.setText(String.format("%s%s%s", p, c, d));
        }).build();
        if (!TextUtils.isEmpty(p) && !TextUtils.isEmpty(c) && !TextUtils.isEmpty(d)) {
            int index1 = provinces.indexOf(p);
            int index2 = cities.get(index1).indexOf(c);
            int index3 = counties.get(index1).get(index2).indexOf(d);
            pvOptions.setSelectOptions(index1, index2, index3);
        }
        pvOptions.setPicker(provinces, cities, counties);
        pvOptions.show();
    }
}
