package com.key.simplekeyboard.activity;

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

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 com.key.simplekeyboard.R;
import com.key.simplekeyboard.SimpleKeyboardSkins;
import com.key.simplekeyboard.tools.Mytools;

public class SettingActivity extends AppCompatActivity {
    private ConstraintLayout step1, step2;
    private ImageView back;
    private ImageView step1_image, step2_image;
    private static final InputMethodManager methodManager = (InputMethodManager) SimpleKeyboardSkins.app.getSystemService(Context.INPUT_METHOD_SERVICE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.setting_activity), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        intView();

        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 intView() {
        back = findViewById(R.id.setting_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);
    }

    private void intEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        step2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodManager.showInputMethodPicker();
                intImage();

            }
        });
        step1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
                startActivity(intent);
                intImage();
            }
        });
    }

    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);
        }
    }

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