package com.photo.keyboard.themes.pager;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;


import com.photo.keyboard.themes.databinding.DialogEnableBinding;
import com.photo.keyboard.themes.manager.EnableActive;

public class DialogEnable extends DialogFragment implements View.OnClickListener {

    private BroadcastReceiver receiver;
    private DialogEnableBinding binding;
    private Context context;

    @Override
    public void onClick(View v) {

        if (v.equals(binding.idStep2)) {
            EnableActive.INSTANCE.onSetEnab();
        } else if (v.equals(binding.idStep1)) {
            EnableActive.INSTANCE.toSet(context);
        }
    }

    public DialogEnable(Context context) {
        this.context = context;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogEnableBinding.inflate(getLayoutInflater());
        onInit();
        return binding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.setCanceledOnTouchOutside(true);
            Window window = dialog.getWindow();
            if (window != null) {
                window.setGravity(Gravity.BOTTOM);
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        }
    }


    public void onInit() {
        updateUi();
        receiver = new myReceive();
        register();
        binding.idStep1.setOnClickListener(this);
        binding.idStep2.setOnClickListener(this);
    }

    class myReceive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            updateUi();
        }
    }


    private void register() {

        context.registerReceiver(receiver, new IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        context.unregisterReceiver(receiver);
    }

    public void updateUi() {
        binding.idStep1.setSelected(EnableActive.INSTANCE.toEnable());
        binding.idStep2.setSelected(EnableActive.INSTANCE.onsetInput());
        boolean selected = binding.idStep1.isSelected();
        boolean selected1 = binding.idStep2.isSelected();
        if (selected) {
            binding.idStep1.setEnabled(false);
        }
        if (selected1) {
            binding.idStep2.setEnabled(false);
        }
        if (selected && selected1) {
            dismiss();
        }

    }
}
