package com.key.coolkeyboard.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

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

import com.key.coolkeyboard.CoolKeyboardSkin;
import com.key.coolkeyboard.R;
import com.key.coolkeyboard.callback.StepDialogCallback;
import com.key.coolkeyboard.databinding.DialogBinding;
import com.key.coolkeyboard.tool.Mytool;

public class StepDialogFragment extends DialogFragment {

    private Activity mActivity;
    private DialogBinding vb;
    private BroadcastReceiver broadcastReceiver;
    private InputMethodManager methodManager;
    private StepDialogCallback callback;


    public StepDialogFragment(Activity activity, StepDialogCallback stepDialogCallback) {
        mActivity = activity;
        callback = stepDialogCallback;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (broadcastReceiver != null && mActivity != null && !mActivity.isFinishing()) {
            mActivity.unregisterReceiver(broadcastReceiver);
        }
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);

        if (callback != null) {
            callback.onDisMiss();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        vb = DialogBinding.inflate(inflater, container, false);
        init();
        return vb.getRoot();
    }

    private void init() {
        methodManager = (InputMethodManager) CoolKeyboardSkin.app.getSystemService(Context.INPUT_METHOD_SERVICE);
        Dialog dialog = getDialog();
        setCancelable(false);
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawableResource(R.color.transparent);
                window.getDecorView().setPadding(0, 0, 0, 0);

                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.gravity = Gravity.BOTTOM;
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(attributes);
            }
        }

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                updateDialogContent();
            }
        };
        mActivity.registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED));


        vb.step1.setOnClickListener(v -> {
            Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
            startActivity(intent);
        });
        vb.step2.setOnClickListener(v -> methodManager.showInputMethodPicker());
        vb.settingCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        updateDialogContent();
    }

    private void updateDialogContent() {
        boolean step1 = Mytool.isStep1();
        boolean step2 = Mytool.isStep2();
        vb.step1.setSelected(step1);
        vb.step2.setSelected(step2);
        if (step1) {
            vb.step1Image.setVisibility(View.VISIBLE);
        } else {
            vb.step1Image.setVisibility(View.GONE);
        }

        if (step2) {
            vb.step2Image.setVisibility(View.VISIBLE);
        } else {
            vb.step2Image.setVisibility(View.GONE);
        }
        if (step1 && step2) {
            dismiss();
        }
    }


    public static StepDialogFragment newInstance(Activity activity,StepDialogCallback stepDialogCallback) {

        StepDialogFragment stepDialogFragment = new StepDialogFragment(activity,stepDialogCallback);
        return stepDialogFragment;
    }
}
