package com.example.iot_controlhost.ui.fragment;

import android.graphics.Color;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.adapter.ManualControllerAdapter;
import com.example.iot_controlhost.base.BaseFragment;
import com.example.iot_controlhost.base.BaseMode;
import com.example.iot_controlhost.databinding.FragmentModelManualBinding;
import com.example.iot_controlhost.databinding.ItemControllerBinding;
import com.example.iot_controlhost.model.controller.AirConditionInfrared;
import com.example.iot_controlhost.model.controller.Controller;
import com.example.iot_controlhost.model.controller.Floor;
import com.example.iot_controlhost.model.controller.relay.Relay;
import com.example.iot_controlhost.ui.dialog.AirDialog;
import com.example.iot_controlhost.ui.dialog.FloorDialog;
import com.example.iot_controlhost.utils.control.infrared.MyInfraredUtils;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSensor;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.global.Variable;
import com.example.iot_controlhost.utils.log.MyLog;

import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * 手动模式
 */
public class ManualFragment extends BaseFragment<FragmentModelManualBinding, ManualFragmentPresenter> implements BaseMode {
    private static ManualControllerAdapter manualControllerAdapter;
//    private static ManualFragment INSTANCE = new ManualFragment();
    Floor floor;

//    public ManualFragment() {
//    }

//    public static ManualFragment getInstance() {
//        return INSTANCE;
//    }

    @Override
    public void init() {
        floor = RoomController.floor;
        binding.setVm(presenter);
        presenter.getRefreshRelay().observe(this, unused -> refreshManualView());
        // 初始化继电器开关设备
        manualControllerAdapter = new ManualControllerAdapter(mContext, null, ItemControllerBinding.class);
        binding.recycleView.setAdapter(manualControllerAdapter);
        binding.recycleView.setLayoutManager(new GridLayoutManager(mContext, 3));
        List<Controller> devices = RoomController.getAllControlDevices();
        int size = devices.size();
        if (size < 6) {
            for (int i = 0; i < 6 - size; i++) {
                devices.add(new Relay("未配置"));
            }
        }
        //初始化控制器显示
        manualControllerAdapter.setDataList(devices);
        if (RoomSetting.getMode() == 0) {
            initMode();
        }
    }

    @Override
    public void initListener() {
        super.initListener();
        binding.cardFloor.setOnClickListener(v -> {
            if (!RoomSensor.dynamicFloorTSensorList.isEmpty() && RoomSensor.getFloorTemperature() > AutoModelSet.getAutoFlorMaxTemp()) {
                Toasty.info(mContext, "当前地面温度已超38℃，请注意使用").show();
            }
            new FloorDialog(mContext).show();
        });
        binding.cardAir.setOnClickListener(v -> new AirDialog(mContext,MyInfraredUtils.getInstances()).show());
    }

    @Override
    public void initMode() {
        MyLog.app("模式生命周期：进入手动模式");
        refreshManualView();
    }

    @Override
    public void stopMode() {
        MyLog.app("模式生命周期：退出手动模式并关闭所有设备");
        RoomController.closeAllControllers();
    }

    /**
     * 刷新手动视图
     */
    private void refreshManualView() {
        //初始化空调地暖视图
        changeFloorState();
        changeAirState();
        //初始化手动控制器显示
        manualControllerAdapter.notifyDataSetChanged();
    }

    private void changeFloorState() {
        binding.tvFloor.setText(RoomSetting.getFloorName());
        binding.tvStateFloor.setText(floor.getState());
        if (floor.isPowerSupply()) {
            binding.ivControllerFloor.setImageResource(R.mipmap.floor_open);
            binding.cardFloor.setBackgroundColor(Color.parseColor("#FF9C00"));
            binding.tvStateFloor.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvFloor.setTextColor(Color.parseColor("#FFFFFF"));
            binding.ivPowerFloor.setBackground(mContext.getDrawable(R.drawable.bg_controller_open));
            binding.ivPowerFloor.setImageDrawable(mContext.getDrawable(R.mipmap.switch_on));
            binding.tvPowerFloor.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvFloorT.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            binding.ivControllerFloor.setImageResource(R.mipmap.floor_close);
            binding.cardFloor.setBackgroundColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#33DCE3E3" : "#F2F0F1"));
            binding.tvStateFloor.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.ivPowerFloor.setBackground(mContext.getDrawable(R.drawable.bg_controller_close));
            binding.ivPowerFloor.setImageDrawable(mContext.getDrawable(R.mipmap.switch_off));
            binding.tvFloor.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvPowerFloor.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvFloorT.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
        }
        binding.tvPowerFloor.setVisibility(RoomController.relay.getModel().equals(RoomController.relay.getModels()[2]) ? View.VISIBLE : View.GONE);
        binding.tvPowerFloor.setText("实时功率：" + RoomController.relayFloor.getPower() + "w");
    }

    private void changeAirState() {
        AirConditionInfrared air = MyInfraredUtils.getInstances();
        binding.tvStateAir.setText(air.getState());
        if (air.isPowerSupply()) {
            binding.ivControllerAir.setImageResource(R.mipmap.air_open);
            binding.cardAir.setBackgroundColor(Color.parseColor(air.getMode() == AirConditionInfrared.HEAT ? "#FF9C00" :
                    air.getMode() == AirConditionInfrared.COLD ? "#00A1FF" : "#7DFA5B"));
            binding.tvStateAir.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvAir.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvPowerAir.setTextColor(Color.parseColor("#FFFFFF"));
            binding.ivPowerAir.setBackground(mContext.getDrawable(R.drawable.bg_controller_open));
            binding.ivPowerAir.setImageDrawable(mContext.getDrawable(R.mipmap.switch_on));
        } else {
            binding.ivControllerAir.setImageResource(R.mipmap.air_close);
            binding.cardAir.setBackgroundColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#33DCE3E3" : "#F2F0F1"));
            binding.tvStateAir.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvAir.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvPowerAir.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.ivPowerAir.setBackground(mContext.getDrawable(R.drawable.bg_controller_close));
            binding.ivPowerAir.setImageDrawable(mContext.getDrawable(R.mipmap.switch_off));
        }
        binding.tvPowerAir.setVisibility(RoomController.relay.getModel().equals(RoomController.relay.getModels()[2]) ? View.VISIBLE : View.GONE);
        binding.tvPowerAir.setText("实时功率：" + RoomController.airCondition.getPower() + "w");
    }
}
