package com.example.iot_controlhost.adapter;

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

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseRecyclerAdapter;
import com.example.iot_controlhost.databinding.ItemControllerBinding;
import com.example.iot_controlhost.model.Result;
import com.example.iot_controlhost.model.controller.Controller;
import com.example.iot_controlhost.model.controller.Dehumidifier485;
import com.example.iot_controlhost.model.controller.relay.Relay;
import com.example.iot_controlhost.model.sensor.GasSensor;
import com.example.iot_controlhost.model.thread.QueueIOUITask;
import com.example.iot_controlhost.ui.dialog.Dehumidifier485Dialog;
import com.example.iot_controlhost.utils.DialogUtil;
import com.example.iot_controlhost.utils.MyQueue;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.log.UserLog;

import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * 手动控制设备列表。
 */
public class ManualControllerAdapter extends BaseRecyclerAdapter<Controller, ItemControllerBinding> {
    Context mContext;

    public ManualControllerAdapter(Context context, List<Controller> dataList, Class<ItemControllerBinding> bindingClass) {
        super(dataList, bindingClass);
        this.mContext = context;
    }

    @Override
    protected void bindData(ItemControllerBinding binding, Controller device) {
        if (device.getName().equals(RoomController.fan.getName())) {
            binding.tvInfo.setText(GasSensor.getQualityAir());
            binding.tvInfo.setTextColor(GasSensor.getQualityAirColor());
        } else {
            binding.tvInfo.setText(null);
        }
        changeState(binding, device);
        binding.tvController.setText(getDisplayName(device));
        binding.cardController.setOnClickListener(view -> {
            if (device instanceof Dehumidifier485) {
                new Dehumidifier485Dialog(mContext, (Dehumidifier485) device).show();
                return;
            }
            DialogUtil.showLoadingDialog(mContext, mContext.getString(R.string.in_operate));
            MyQueue.start(MyQueue.TYPE_CONTROLLER, new QueueIOUITask<Result>() {
                @Override
                public Result doInQueueThread() throws Exception {
                    Result result = new Result();
                    result.setOperateResult(device.setPowerSupply(!device.isPowerSupply()));
                    result.setState(device.isPowerSupply());
                    return result;
                }

                @Override
                public void doInUIThread(Result result) {
                    if (result.isOperateResult()) {
                        changeState(binding, device);
                        Toasty.success(mContext, mContext.getString(R.string.operate_success)).show();
                        UserLog.operate("本机屏幕", "设备操作成功："
                                + (result.isState() ? "开启" : "关闭") + device.getName());
                    } else {
                        Toasty.error(mContext, mContext.getString(R.string.operate_fail)).show();
                        UserLog.operateError("本机屏幕", "设备操作失败："
                                + (result.isState() ? "开启" : "关闭") + device.getName());
                    }
                    DialogUtil.hideLoadingDialog();
                }
            });
        });
    }

    private void changeState(ItemControllerBinding binding, Controller device) {
        if ("未配置".equals(device.getName())) {
            binding.cardNo.setVisibility(View.VISIBLE);
            binding.cardYes.setVisibility(View.GONE);
            return;
        } else {
            binding.cardNo.setVisibility(View.GONE);
            binding.cardYes.setVisibility(View.VISIBLE);
        }
        if (device.isPowerSupply()) {
            binding.tvState.setText("运行中");
            binding.ivController.setImageResource(getIconOpen(device));
            binding.ivPower.setBackground(mContext.getDrawable(R.drawable.bg_controller_open));
            binding.ivPower.setImageDrawable(mContext.getDrawable(R.mipmap.switch_on));
            binding.cardController.setBackgroundColor(getOpenColor(device));
            binding.ivController.setImageDrawable(mContext.getDrawable(getIconOpen(device)));
            binding.tvState.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvController.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvPower.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            binding.tvState.setText("未启动");
            binding.ivController.setImageResource(getIconClose(device));
            binding.cardController.setBackgroundColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#33DCE3E3" : "#F2F0F1"));
            binding.ivPower.setBackground(mContext.getDrawable(R.drawable.bg_controller_close));
            binding.ivPower.setImageDrawable(mContext.getDrawable(R.mipmap.switch_off));
            binding.ivController.setImageDrawable(mContext.getDrawable(getIconClose(device)));
            binding.tvState.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvController.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvPower.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
        }
        if (device instanceof Dehumidifier485 && RoomController.relay.getModel().equals(RoomController.relay.getModels()[2])) {
            binding.tvPower.setVisibility(View.VISIBLE);
            binding.tvPower.setText("实时功率：" + RoomController.dehumidifier485Relay.getPowerText() + "w");
        } else if (device instanceof Relay && RoomController.relay.getModel().equals(RoomController.relay.getModels()[2])) {
            binding.tvPower.setVisibility(View.VISIBLE);
            binding.tvPower.setText("实时功率：" + ((Relay) device).getPowerText() + "w");
        } else {
            binding.tvPower.setVisibility(View.GONE);
        }
    }

    private int getIconOpen(Controller device) {
        if (device instanceof Relay) {
            return ((Relay) device).getIconOpen();
        }
        if (device instanceof Dehumidifier485) {
            return R.mipmap.ven_open;
        }
        return R.mipmap.open;
    }

    private int getIconClose(Controller device) {
        if (device instanceof Relay) {
            return ((Relay) device).getIconClose();
        }
        if (device instanceof Dehumidifier485) {
            return R.mipmap.ven_close;
        }
        return R.mipmap.close;
    }

    private int getOpenColor(Controller device) {
        if (device instanceof Dehumidifier485) {
            return Color.parseColor("#00A1FF");
        }
        return Color.parseColor("#00BDF4");
    }

    private String getDisplayName(Controller device) {
        if (device instanceof Dehumidifier485) {
            return ((Dehumidifier485) device).getDisplayName();
        }
        return device.getName();
    }
}
