package com.example.chemical.ui.add.locker;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.LogUtils;
import com.example.chemical.databinding.FragmentLockerBinding;
import com.example.chemical.ui.add.FragmentCallBack;
import com.rc.httpcore.bean.locker.HxpCabinetDoorVo;
import com.rc.httpcore.bean.locker.HxpCabinetVo;

import java.util.LinkedList;
import java.util.List;

public class LockerFragment extends Fragment {
    private FragmentLockerBinding binding;
    private HxpCabinetVo hxpCabinetVo;
    // 所有柜层
    private List<DrawerUiBean> drawerUiList = new LinkedList<>();
    private FragmentCallBack fragmentCallBack;

    public LockerFragment(HxpCabinetVo hxpCabinetVo, FragmentCallBack fragmentCallBack) {
        this.hxpCabinetVo = hxpCabinetVo;
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer1RB, binding.lockerUpDrawer1TV, false));
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer2RB, binding.lockerUpDrawer2TV, false));
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer3RB, binding.lockerUpDrawer3TV, false));
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer4RB, binding.lockerUpDrawer4TV, false));
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer5RB, binding.lockerUpDrawer5TV, false));
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer6RB, binding.lockerUpDrawer6TV, false));
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer7RB, binding.lockerUpDrawer7TV, false));
        drawerUiList.add(new DrawerUiBean(binding.lockerUpDrawer8RB, binding.lockerUpDrawer8TV, false));
        // 所有柜门
        List<HxpCabinetDoorVo> doorVoList = hxpCabinetVo.getCabinetDoorVoList();
        String[] drawerArray = new String[]{"①  层", "②  层", "③  层", "④  层", "⑤  层", "⑥  层", "⑦  层", "⑧  层"};
        if (doorVoList.size() == 1) {
            HxpCabinetDoorVo hxpCabinetDoorVo = doorVoList.get(0);
            // 层数
            int doorLayers = hxpCabinetDoorVo.getDoorLayers();
            for (int i = 0; i < 8; i++) {
                DrawerUiBean drawerUiBean = drawerUiList.get(i);
                drawerUiBean.setVisible(i < doorLayers);
            }
            LogUtils.dTag("DEBUG", drawerUiList);
            for (int i = 0; i < drawerUiList.size(); i++) {
                DrawerUiBean drawerUiBean = drawerUiList.get(i);
                if (drawerUiBean.isVisible()) {
                    drawerUiBean.getRadioButton().setVisibility(View.VISIBLE);
                    drawerUiBean.getTextView().setVisibility(View.VISIBLE);
                    drawerUiBean.setDrawer(drawerArray[i]);
                    drawerUiBean.getTextView().setText(drawerUiBean.getDrawer());
                }
            }
        } else if (doorVoList.size() > 1) {
            HxpCabinetDoorVo hxpCabinetDoorUpVo = doorVoList.get(0);
            // 层数
            int doorUpLayers = hxpCabinetDoorUpVo.getDoorLayers();
            for (int i = 0; i < 4; i++) {
                DrawerUiBean drawerUiBean = drawerUiList.get(i);
                drawerUiBean.setVisible(i < doorUpLayers);
            }

            // 设置柜1层高   柜内高484px
            int itemUpH = 484 / 2 / doorUpLayers;
            for (int i = 0; i < doorUpLayers; i++) {
                for (int j = 0; j < 4; j++) {
                    DrawerUiBean drawerUiBean = drawerUiList.get(j);
                    RadioButton radioButton = drawerUiBean.getRadioButton();
                    ViewGroup.LayoutParams radioButtonLayoutParams = radioButton.getLayoutParams();
                    radioButtonLayoutParams.height = itemUpH;
                    radioButton.setLayoutParams(radioButtonLayoutParams);

                    TextView textView = drawerUiBean.getTextView();
                    ViewGroup.LayoutParams textViewLayoutParams = textView.getLayoutParams();
                    textViewLayoutParams.height = itemUpH;
                    textView.setLayoutParams(textViewLayoutParams);
                }
            }


            HxpCabinetDoorVo hxpCabinetDoorDownVo = doorVoList.get(1);
            // 层数
            int doorDownLayers = hxpCabinetDoorDownVo.getDoorLayers();
            for (int i = 4; i < 8; i++) {
                DrawerUiBean drawerUiBean = drawerUiList.get(i);
                drawerUiBean.setVisible(i < (doorDownLayers + 4));
            }

            // 设置柜2层高   柜内高484px
            int itemDownH = 484 / 2 / doorDownLayers;
            for (int i = 0; i < doorDownLayers; i++) {
                for (int j = 4; j < 8; j++) {
                    DrawerUiBean drawerUiBean = drawerUiList.get(j);
                    RadioButton radioButton = drawerUiBean.getRadioButton();
                    ViewGroup.LayoutParams radioButtonLayoutParams = radioButton.getLayoutParams();
                    radioButtonLayoutParams.height = itemDownH;
                    radioButton.setLayoutParams(radioButtonLayoutParams);

                    TextView textView = drawerUiBean.getTextView();
                    ViewGroup.LayoutParams textViewLayoutParams = textView.getLayoutParams();
                    textViewLayoutParams.height = itemDownH;
                    textView.setLayoutParams(textViewLayoutParams);
                }
            }
            LogUtils.dTag("DEBUG", drawerUiList);
            for (int i = 0; i < drawerUiList.size(); i++) {
                DrawerUiBean drawerUiBean = drawerUiList.get(i);
                if (drawerUiBean.isVisible()) {
                    drawerUiBean.getRadioButton().setVisibility(View.VISIBLE);
                    drawerUiBean.getTextView().setVisibility(View.VISIBLE);
                    drawerUiBean.setDrawer(drawerArray[i >= 4 ? i - doorUpLayers : i]);
                    drawerUiBean.getTextView().setText(drawerUiBean.getDrawer());
                } else {
                    drawerUiBean.getRadioButton().setVisibility(View.GONE);
                    drawerUiBean.getTextView().setVisibility(View.GONE);
                }
            }
        }

        binding.lockerUpDrawerRG.setOnCheckedChangeListener((group, checkedId) -> {
            int radioButtonIndex = -1;
            for (int i = 0; i < group.getChildCount(); i++) {
                RadioButton radioButton = (RadioButton) group.getChildAt(i);
                if (radioButton.getId() == checkedId) {
                    radioButtonIndex = i;
                    break;
                }
            }
            if (radioButtonIndex != -1) {
                fragmentCallBack.onSelected(hxpCabinetVo, radioButtonIndex);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
