package tr.dhee.abox.demo.ui.switchcontrol;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import tr.dhee.abox.demo.databinding.ActivitySwitchControlBinding;

public class SwitchControlActivity extends AppCompatActivity {

    private SwitchViewModel switchViewModel;
    private ActivitySwitchControlBinding binding;

    // 设备名称, 可根据实际需要调整
    private static final String DEVICE_NAME = "智能插座";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        binding = ActivitySwitchControlBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(binding.main, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        switchViewModel = new ViewModelProvider(this, new SwitchViewModelFactory())
                .get(SwitchViewModel.class);

        final Button btnOn = binding.btnOn;
        final Button btnOff = binding.btnOff;
        final ProgressBar loadingProgressBar = binding.loading;
        final TextView statusText = binding.statusText;

        // 观察控制结果
        switchViewModel.getSwitchResult().observe(this, switchResult -> {
            if (switchResult == null) {
                return;
            }
            loadingProgressBar.setVisibility(View.GONE);
            btnOn.setEnabled(true);
            btnOff.setEnabled(true);

            if (switchResult.getSuccess() != null) {
                statusText.setVisibility(View.VISIBLE);
                statusText.setText(switchResult.getSuccess());
                statusText.setTextColor(ContextCompat.getColor(SwitchControlActivity.this,
                        android.R.color.holo_green_dark));
                Toast.makeText(this, switchResult.getSuccess(), Toast.LENGTH_SHORT).show();
            }
            if (switchResult.getError() != null) {
                statusText.setVisibility(View.VISIBLE);
                statusText.setText(switchResult.getError());
                statusText.setTextColor(ContextCompat.getColor(SwitchControlActivity.this,
                        android.R.color.holo_red_dark));
                Toast.makeText(this, switchResult.getError(), Toast.LENGTH_SHORT).show();
            }
        });

        // "开" 按钮
        btnOn.setOnClickListener(v -> {
            loadingProgressBar.setVisibility(View.VISIBLE);
            btnOn.setEnabled(false);
            btnOff.setEnabled(false);
            switchViewModel.controlSwitch(DEVICE_NAME, "1");
        });

        // "关" 按钮
        btnOff.setOnClickListener(v -> {
            loadingProgressBar.setVisibility(View.VISIBLE);
            btnOn.setEnabled(false);
            btnOff.setEnabled(false);
            switchViewModel.controlSwitch(DEVICE_NAME, "0");
        });
    }
}
