package com.example.upbot;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.TextView;

public class RobotActivity extends AppCompatActivity {

    private ImageButton btn_quiet, btn_standard, btn_strong, btn_super, btn_water_less, btn_water_medium, btn_water_high;
    private TextView tv_show_power, tv_show_water,tv_quiet, tv_standard, tv_strong, tv_super, tv_water_less, tv_water_medium, tv_water_high;
    int i = -1, j = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot);

        Toolbar toolbar = findViewById(R.id.toolBar_robot);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        Drawable overflowIcon = ContextCompat.getDrawable(this, R.drawable.baseline_more_vert_24);
        if (overflowIcon != null) {
            toolbar.setOverflowIcon(overflowIcon);
        }
        initViews();
        setListeners();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void initViews() {
        btn_quiet = findViewById(R.id.btn_quiet);
        btn_standard = findViewById(R.id.btn_standard);
        btn_strong = findViewById(R.id.btn_strong);
        btn_super = findViewById(R.id.btn_super);
        btn_water_less = findViewById(R.id.btn_water_less);
        btn_water_medium = findViewById(R.id.btn_water_medium);
        btn_water_high = findViewById(R.id.btn_water_high);
        tv_show_power = findViewById(R.id.tv_show_power);
        tv_show_water = findViewById(R.id.tv_show_water);
        tv_quiet = findViewById(R.id.tv_quiet);
        tv_standard = findViewById(R.id.tv_standard);
        tv_strong = findViewById(R.id.tv_strong);
        tv_super = findViewById(R.id.tv_super);
        tv_water_less = findViewById(R.id.tv_water_less);
        tv_water_medium = findViewById(R.id.tv_water_medium);
        tv_water_high = findViewById(R.id.tv_water_high);
    }

    private void setListeners() {
        btn_quiet.setOnClickListener(view -> {
            i = 0;
            check();
        });
        btn_standard.setOnClickListener(view -> {
            i = 1;
            check();
        });
        btn_strong.setOnClickListener(view -> {
            i = 2;
            check();
        });
        btn_super.setOnClickListener(view -> {
            i = 3;
            check();
        });
        btn_water_less.setOnClickListener(view -> {
            j = 0;
            check();
        });
        btn_water_medium.setOnClickListener(view -> {
            j = 1;
            check();
        });
        btn_water_high.setOnClickListener(view -> {
            j = 2;
            check();
        });
    }

    private void check() {
        if (i == 0){
            btn_quiet.setBackground(getDrawable(R.drawable.iv_quiet_pressed));
            tv_quiet.setTextColor(getColor(R.color.text_blue));
            tv_show_power.setText("安静");
        }else {
            btn_quiet.setBackground(getDrawable(R.drawable.iv_quiet));
            tv_quiet.setTextColor(getColor(R.color.text_color));
        }
        if (i == 1){
            btn_standard.setBackground(getDrawable(R.drawable.iv_biaozhun_pressed));
            tv_standard.setTextColor(getColor(R.color.text_blue));
            tv_show_power.setText("标准");
        }else {
            btn_standard.setBackground(getDrawable(R.drawable.iv_biaozhun));
            tv_standard.setTextColor(getColor(R.color.text_color));
        }
        if (i == 2){
            btn_strong.setBackground(getDrawable(R.drawable.iv_strong_pressed));
            tv_strong.setTextColor(getColor(R.color.text_blue));
            tv_show_power.setText("强力");
        }else {
            btn_strong.setBackground(getDrawable(R.drawable.iv_strong));
            tv_strong.setTextColor(getColor(R.color.text_color));
        }
        if (i == 3){
            btn_super.setBackground(getDrawable(R.drawable.iv_super_pressed));
            tv_super.setTextColor(getColor(R.color.text_blue));
            tv_show_power.setText("超强");
        }else {
            btn_super.setBackground(getDrawable(R.drawable.iv_super));
            tv_super.setTextColor(getColor(R.color.text_color));
        }
        if (j == 0){
            btn_water_less.setBackground(getDrawable(R.drawable.iv_short_pressed));
            tv_water_less.setTextColor(getColor(R.color.text_blue));
            tv_show_water.setText("低");
        }else {
            btn_water_less.setBackground(getDrawable(R.drawable.iv_short));
            tv_water_less.setTextColor(getColor(R.color.text_color));
        }
        if (j == 1){
            btn_water_medium.setBackground(getDrawable(R.drawable.iv_medium_pressed));
            tv_water_medium.setTextColor(getColor(R.color.text_blue));
            tv_show_water.setText("中");
        }else {
            btn_water_medium.setBackground(getDrawable(R.drawable.iv_medium));
            tv_water_medium.setTextColor(getColor(R.color.text_color));
        }
        if (j == 2){
            btn_water_high.setBackground(getDrawable(R.drawable.iv_high_pressed));
            tv_water_high.setTextColor(getColor(R.color.text_blue));
            tv_show_water.setText("高");
        }else {
            btn_water_high.setBackground(getDrawable(R.drawable.iv_high));
            tv_water_high.setTextColor(getColor(R.color.text_color));
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}