package com.example.chemical.weidith;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatDialog;

import com.blankj.utilcode.util.ThreadUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.chemical.ChemicalApp;
import com.example.chemical.R;
import com.example.chemical.databinding.DialogBluetoothWeighBinding;
import com.example.chemical.utils.bluetooth.BluetoothTool;
import com.rc.httpcore.HttpConfig;
import com.rc.httpcore.bean.HxpChemicalVo;

public class BluetoothWeighDialog extends AppCompatDialog {
    private DialogBluetoothWeighBinding binding;
    private final Context context;
    private final HxpChemicalVo hxpChemicalVo;

    public BluetoothWeighDialog(Context context, HxpChemicalVo hxpChemicalVo) {
        super(context);
        this.context = context;
        this.hxpChemicalVo = hxpChemicalVo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DialogBluetoothWeighBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Window window = getWindow();
        if (null != window) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER);
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }

        // 提示图片加载
        if (null != ChemicalApp.confs && ChemicalApp.confs.getWeighHintPicture() != null && !ChemicalApp.confs.getWeighHintPicture().isEmpty()) {
            Glide.with(context)
                    .load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getWeighHintPicture())
                    .placeholder(R.mipmap.img_syt_cz)
                    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                    .into(binding.hintIV);
        }
        // 净含量
        binding.netWtET.setText(String.valueOf(hxpChemicalVo.getNetWt()));
        // 规格
        binding.specsTV.setText(hxpChemicalVo.getSpecNum() + hxpChemicalVo.getSpecUnit());
        // 蓝牙称
        ThreadUtils.executeByCached(weightTask);
        // 确认
        binding.confirmBT.setOnClickListener(v -> {
            Editable netWtETText = binding.netWtET.getText();
            if (null == netWtETText || TextUtils.isEmpty(netWtETText)) {
                binding.netWtET.setError("请输入净含量");
                return;
            }
            double netWt = -1.0;
            try {
                netWt = Double.parseDouble(netWtETText.toString());
            } catch (Exception e) {
                binding.netWtET.setError("请检查净含量内容正确");
                return;
            }
            if (netWt <= 0) {
                binding.netWtET.setError("请检查净含量内容正确");
                return;
            }
            hxpChemicalVo.setNetWt(netWt);
            dismiss();
        });

    }

    @Override
    protected void onStop() {
        super.onStop();
        BluetoothTool.INSTANCE.disconnect();
        ThreadUtils.cancel(weightTask);
    }

    ThreadUtils.SimpleTask<Object> weightTask = new ThreadUtils.SimpleTask<Object>() {
        @Override
        public Object doInBackground() throws Throwable {
            BluetoothTool.INSTANCE.connect(weight -> {
                hxpChemicalVo.setWeight(weight);
                ThreadUtils.runOnUiThread(() -> binding.weighTV.setText(weight + "g"));
            });
            return null;
        }

        @Override
        public void onSuccess(Object result) {

        }
    };
}
