package com.example.applock.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.example.applock.DrawView.InPutKeyView;
import com.example.applock.R;
import com.example.applock.allmanager.Datamanager;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.listener.InputKeyListener;
import com.example.applock.value.Adcallback;
import com.example.applock.value.MyAllValues;
import com.example.applock.value.Mytool;

import java.util.List;
import java.util.Objects;

public class KeyWordActivity extends AppCompatActivity {

    private TextView text_key_btn;
    private ImageView image_back;
    private TextView text_page;
    private String fistkey;
    private InPutKeyView inPutKeyView;
    private int MyKeyPage;
    private List<ATInterstitial> ads;
    private int type_from;
    private TextView key_top_title;

    private ImageView key_page_logo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyKeyPage = getIntent().getIntExtra(MyAllValues.PASS_KEY_WORD, 0);
        Mytool.getAllAd();
        setContentView(R.layout.activity_key_word);
        ads = SoftWareLockApplication.lodAd();
        text_page = findViewById(R.id.text_page);
        text_key_btn = findViewById(R.id.text_key_btn);
        image_back = findViewById(R.id.image_back);
        inPutKeyView = findViewById(R.id.input_view);
        key_top_title = findViewById(R.id.key_top_title);
        key_page_logo = findViewById(R.id.key_page_logo);
        gotoKyePage();
    }

    private void gotoKyePage() {
        if (MyKeyPage == 0) {
            image_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog alertDialog2 = new AlertDialog.Builder(KeyWordActivity.this)
                            .setTitle("Alert")
                            .setMessage(getString(R.string.alert_worn))
                            .setIcon(R.mipmap.logo)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                }
                            })

                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//添加取消
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                }
                            })
                            .create();
                    alertDialog2.show();
                }
            });
            text_page.setText(getString(R.string.set_key));
            text_key_btn.setText(getString(R.string.next));
            image_back.setVisibility(View.VISIBLE);
            text_key_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(KeyWordActivity.this, KeyWordActivity.class);
                    intent.putExtra(MyAllValues.PASS_KEY_FROM,0);
                    intent.putExtra(MyAllValues.PASS_KEY_WORD, 1);
                    intent.putExtra(MyAllValues.PASS_WORD_TOP_KEY, fistkey);
                    startActivity(intent);
                    finish();
                }
            });
        } else if (MyKeyPage == 1) {
            text_page.setText(getString(R.string.confirm));
            text_key_btn.setText(getString(R.string.go_on));
            image_back.setVisibility(View.VISIBLE);
            String setPin = getIntent().getStringExtra(MyAllValues.PASS_WORD_TOP_KEY);
            text_key_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type_from = getIntent().getIntExtra(MyAllValues.PASS_KEY_FROM,0);
                    if (type_from == 0){
                        if (Objects.equals(setPin, fistkey)) {
                            Datamanager.setTopKey(fistkey);
                            Intent intent = new Intent(KeyWordActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(KeyWordActivity.this, getString(R.string.set_key_file), Toast.LENGTH_SHORT).show();
                            inPutKeyView.clearTopKey();
                            text_key_btn.setVisibility(View.GONE);
                        }
                    }else {
                        if (Objects.equals(setPin, fistkey)) {
                            Datamanager.setTopKey(fistkey);
                            Toast.makeText(KeyWordActivity.this, getString(R.string.reset_success), Toast.LENGTH_SHORT).show();
                            showAd();
                        } else {
                            Toast.makeText(KeyWordActivity.this, getString(R.string.set_key_file), Toast.LENGTH_SHORT).show();
                            inPutKeyView.clearTopKey();
                            text_key_btn.setVisibility(View.GONE);
                        }
                    }

                }
            });
            image_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } else if (MyKeyPage == 2) {
            image_back.setVisibility(View.VISIBLE);
            text_key_btn.setText(getString(R.string.next));
            key_top_title.setVisibility(View.VISIBLE);
            text_page.setVisibility(View.GONE);
            key_page_logo.setVisibility(View.VISIBLE);
            text_key_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(KeyWordActivity.this, KeyWordActivity.class);
                    intent.putExtra(MyAllValues.PASS_KEY_FROM,2);
                    intent.putExtra(MyAllValues.PASS_KEY_WORD, 1);
                    intent.putExtra(MyAllValues.PASS_WORD_TOP_KEY, fistkey);
                    startActivity(intent);
                    finish();
                }
            });
            image_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        inPutKeyView.setInputKeyListener(new InputKeyListener() {
            @Override
            public void onInputComplete(String lockkey) {
                text_key_btn.setVisibility(View.VISIBLE);
                fistkey = lockkey;
            }

            @Override
            public void onInputUnComplete(String lockkey) {
                text_key_btn.setVisibility(View.GONE);
                fistkey = lockkey;

            }
        });
    }



    private void showAd() {
        ATInterstitial mInterstitialAd = Mytool.onCache(ads);
        if (mInterstitialAd == null) {
            finish();
        } else {
            Mytool.setCallback(mInterstitialAd, new Adcallback() {
                @Override
                public void onShowFail(AdError ad) {
                    finish();
                }

                @Override
                public void onAdHidden() {
                    finish();
                }
            });
            mInterstitialAd.show(this);
            finish();
        }

    }

}