package com.exquisite.activity;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.exquisite.KeyboardSkin;
import com.exquisite.R;
import com.exquisite.tools.Mytools;

public class KeyBoardSettingsActivity extends AppCompatActivity implements View.OnClickListener {
    private static final InputMethodManager methodManager = (InputMethodManager) KeyboardSkin.app.getSystemService(Context.INPUT_METHOD_SERVICE);
    private LinearLayout back, step1, step2;
    private ImageView step1_image, step2_image;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_board_settings);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.keyboard_settings), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        intView();
        intImage();
        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                step1.setSelected(Mytools.isStep1());
                step2.setSelected(Mytools.isStep2());
                intImage();
            }
        };
        registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED));


        intEvent();
    }

    private void intImage() {
        if (Mytools.isStep2()) {
            step2_image.setVisibility(View.VISIBLE);
        } else {
            step2_image.setVisibility(View.GONE);
        }
        if (Mytools.isStep1()) {
            step1_image.setVisibility(View.VISIBLE);
        } else {
            step1_image.setVisibility(View.GONE);
        }
    }

    private void intEvent() {
        back.setOnClickListener(this);
        step2.setOnClickListener(this);
        step1.setOnClickListener(this);
    }

    private void intView() {
        back = findViewById(R.id.settings_back);
        step1 = findViewById(R.id.step1);
        step2 = findViewById(R.id.step2);
        step1_image = findViewById(R.id.step1_image);
        step2_image = findViewById(R.id.step2_image);
        step1_image.setVisibility(View.GONE);
        step2_image.setVisibility(View.GONE);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.settings_back:
                finish();
                break;
            case R.id.step1:
                Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
                startActivity(intent);
                intImage();
                break;
            case R.id.step2:
                methodManager.showInputMethodPicker();
                intImage();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        step1.setSelected(Mytools.isStep1());
        step2.setSelected(Mytools.isStep2());
        intImage();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Mytools.isStep1() && Mytools.isStep2()) {
            Toast.makeText(KeyBoardSettingsActivity.this, getString(R.string.text_step_successful), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(KeyBoardSettingsActivity.this, getString(R.string.text_step_failed), Toast.LENGTH_SHORT).show();
        }
    }
}