package com.key.coolkeyboard.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.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.key.coolkeyboard.CoolKeyboardSkin;
import com.key.coolkeyboard.R;
import com.key.coolkeyboard.databinding.ActivitySettingBinding;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.topon.AdManager;

public class SettingActivity extends AppCompatActivity {
    ActivitySettingBinding binding;
    private BroadcastReceiver broadcastReceiver;
    private static final InputMethodManager methodManager = (InputMethodManager) CoolKeyboardSkin.app.getSystemService(Context.INPUT_METHOD_SERVICE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_setting), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        AdManager.loadAllAd();
        binding.settingTitle.setTypeface(Typeface.createFromAsset(getAssets(), "Baumans-Regular.ttf"));


        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                boolean step1 = Mytool.isStep1();
                boolean step2 = Mytool.isStep2();
                binding.step1.setSelected(step1);
                binding.step2.setSelected(step2);
                intImage();
            }
        };
        registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED));
        initEvent();


    }

    private void initEvent() {

        binding.settingBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

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

        binding.step2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodManager.showInputMethodPicker();
            }
        });

    }

    private void intImage() {
        if (Mytool.isStep2()) {
            binding.step2Image.setVisibility(View.VISIBLE);
        } else {
            binding.step2Image.setVisibility(View.GONE);
        }
        if (Mytool.isStep1()) {
            binding.step1Image.setVisibility(View.VISIBLE);
        } else {
            binding.step1Image.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean step1 = Mytool.isStep1();
        boolean step2 = Mytool.isStep2();

        binding.step1.setSelected(step1);
        binding.step2.setSelected(step2);
        intImage();

        if (step1 && step2) {
            Intent intent = new Intent(SettingActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }
}