package com.swiftcleaner.chovey.view.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.databinding.ActivityInfoBinding;
import com.swiftcleaner.chovey.model.util.BatteryReceiver;
import com.swiftcleaner.chovey.view.dialog.CustomScoreDialog;

public class InfoActivity extends AppCompatActivity {
    private ActivityInfoBinding binding;
    private BatteryReceiver batteryReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityInfoBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        initBar();
        initShow();
        registerReceiver();
    }

    private void initBar() {
        ImmersionBar.with(this).transparentStatusBar()  // 透明状态栏
                .statusBarDarkFont(false)  // 状态栏字体颜色为浅色
                .navigationBarDarkIcon(true)  // 导航栏图标颜色为浅色
                .init();
    }

    private void initShow() {
        binding.ivReturn.setOnClickListener(v -> {
            initCustDialog();
        });
        binding.infoButtonOk.setOnClickListener(v->{
            initCustDialog();
        });
    }

    private void registerReceiver() {
        batteryReceiver = new BatteryReceiver(new Runnable() {
            @Override
            public void run() {
                setBatteryInfo();
            }
        });
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver, intentFilter);
    }

    private void setBatteryInfo() {
        binding.temperatureNum.setText(String.format("%.1f ℃",batteryReceiver.temperature / 10.0));
        binding.voltageNum.setText(String.format("%.1f V", batteryReceiver.voltage / 1000.0));
        binding.technologyNum.setText(batteryReceiver.technology);
        // 格式化显示 batteryCapacityNum，保留整数
        binding.batteryCapacityNum.setText(String.format("%d mAh", (int) batteryReceiver.mAh));
        binding.electricNum.setText(String.format("%.2f mA", batteryReceiver.current / 1000.0));
        binding.currentNum.setText(String.format("%.2f mA", batteryReceiver.currentAverage / 1000.0));
        binding.infoHour.setText(String.valueOf(batteryReceiver.hours));
        binding.infoMinute.setText(String.valueOf(batteryReceiver.minutes));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (batteryReceiver != null) {
            unregisterReceiver(batteryReceiver);
        }
    }

    @Override
    public void onBackPressed() {
        initCustDialog();
    }

    private void initCustDialog() {
        CustomScoreDialog customScoreDialog = new CustomScoreDialog(this);
        customScoreDialog.setOnDialogClickListener(new CustomScoreDialog.OnDialogClickListener() {
            @Override
            public void onCancel() {
                customScoreDialog.dismiss();
            }

            @Override
            public void onSubmit() {
                customScoreDialog.dismiss();
                finish();
            }
        });
        customScoreDialog.show();
    }
}