package com.example.iot_controlhost.ui.activity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.adapter.SensorInfoAdapter;
import com.example.iot_controlhost.base.BaseActivity;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.ActivityInfoBinding;
import com.example.iot_controlhost.databinding.ItemSensorInfoBinding;
import com.example.iot_controlhost.model.Silkworm;
import com.example.iot_controlhost.model.SilkwormNew;
import com.example.iot_controlhost.ui.dialog.ConfirmDialog;
import com.example.iot_controlhost.ui.dialog.EnergyDialog;
import com.example.iot_controlhost.utils.AppUpdateUtil;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.QRCodeUtil;
import com.example.iot_controlhost.utils.database.SilkwormDBManager;
import com.example.iot_controlhost.utils.global.HardwareSetting;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.network.PublicNetRequest;

import es.dmoral.toasty.Toasty;

public class InfoActivity extends BaseActivity<ActivityInfoBinding, InfoActivityPresenter> {

    SensorInfoAdapter sensorInfoAdapter;

    @Override
    public void initView() {
        sensorInfoAdapter = new SensorInfoAdapter(null, ItemSensorInfoBinding.class);
        //初始化传感器详细数据显示框架
        binding.recycleView.setLayoutManager(new LinearLayoutManager(this));
        binding.recycleView.setAdapter(sensorInfoAdapter);
        //观察显示最新传感器数值
        presenter.getSensorInfoList().observe(this, list -> sensorInfoAdapter.setDataList(list));
        binding.tvVersion.setText(MyUtil.getVersionId(this) + "-" + MyUtil.getVersionName(this));
        binding.tvDeviceId.setText(HardwareSetting.getHostId());
        //加载二维码
        PublicNetRequest.loadSignImage(this, binding.ivQrcode);
        //初始化基础数据
        initData();
        binding.setVm(presenter);
        presenter.getNoticeLiveData().observe(this, integer -> {
            switch (integer) {
                case 0 -> //关闭软件
//                        System.out.println(1/0);
                        new ConfirmDialog(mContext, getString(R.string.warning), getString(R.string.ask_shutdown), () -> MyUtil.shutDown()).show();
                case 1 -> //重启APP
                        new ConfirmDialog(mContext, getString(R.string.warning), getString(R.string.ask_reboot), () -> MyUtil.relaunchApp()).show();
                case 2 -> //显示硬件码的二维码
                        showConfirmDialog(QRCodeUtil.createQRCodeBitmap(HardwareSetting.getHostId(), 300, 300, null, 30));
                case 4 -> //检查更新
                        AppUpdateUtil.updateAPP(mContext, false, false);
                case 5 -> //关闭页面
                        finish();
            }
        });
    }

    @Override
    public void initListener() {
        super.initListener();
        binding.cardRenew.setOnClickListener(v -> {
//            new RenewDialog(mContext).show();//todo 续费功能
            Toasty.info(mContext, getString(R.string.developing,"续费")).show();
        });
        binding.tvEnergy.setOnLongClickListener(v -> {
            if(RoomController.relay.getModel().equals(RoomController.relay.getModels()[2])){
                new EnergyDialog(mContext).show();
            } else {
                Toasty.info(mContext, "仅BBIT继电器支持查看能耗详情").show();
            }
            return true;
        });
    }

    /**
     * 初始化固定数据
     */
    private void initData() {
        //初始化共育状态
        fillDataInText(binding.tvHead, RoomSetting.getRoomName());
        fillDataInText(binding.tvSim, HardwareSetting.getIccid());
        if (SilkwormDBManager.isStarting()) {
            //正在共育中
            SilkwormNew silkworm = SilkwormDBManager.getLatestSilkworm();
            fillDataInText(binding.tvAge, SilkwormDBManager.getStartToNowDays());
            fillDataInText(binding.tvVariety, silkworm.getVariety());
            fillDataInText(binding.tvSeason, silkworm.getName());
        }
    }

    /**
     * 显示二维码弹窗
     *
     * @param bitmap 图片内容
     */
    public void showConfirmDialog(Bitmap bitmap) {
        Dialog dialog = new Dialog(mContext, R.style.MyDialogTheme);
        dialog.setContentView(R.layout.dialog_host_id);
        dialog.findViewById(R.id.iv_dialog_close).setOnClickListener(view -> dialog.dismiss());
        dialog.findViewById(R.id.btn_yes).setOnClickListener(view -> dialog.dismiss());
        dialog.setCanceledOnTouchOutside(true);
        ((ImageView) dialog.findViewById(R.id.iv_pic)).setImageBitmap(bitmap);
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.getWindow().setAttributes(BaseDialog.getAttributes(dialog));
        dialog.show();
    }

}
