package com.lc.myapplication.security.locks.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.lc.myapplication.security.locks.listener.ActionListener;
import com.lc.myapplication.security.locks.max.MaxManager;
import com.lc.myapplication.security.locks.max.onAdStatusListener;
import com.lc.myapplication.security.locks.tool.MyTools;
import com.lc.myapplication.security.locks.R;
import com.lc.myapplication.security.locks.draw.DrawInputView;
import com.lc.myapplication.security.locks.listener.InputCodeFinishListener;
import com.lc.myapplication.security.locks.value.MeValues;

import java.util.Objects;

public class PassCodeActivity extends AppCompatActivity {
    private LinearLayout passcode_main;
    private TextView passcode_name;
    private TextView passcode_step;
    private int type;
    private int type_from;
    private String inputkey;
    private DrawInputView drawInputView;
    private ImageView image_back;

    private boolean showAd = false;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_passcode_word);
        MaxManager.onLoadAd();
        type = getIntent().getIntExtra(MeValues.KEYCODE_TYPE, 0);
        showAd = getIntent().getBooleanExtra(MeValues.KEY_SHOW, false);
        if (showAd) {
            showMax(new ActionListener() {
                @Override
                public void onAction() {

                }
            });
        }
        initfindview();
        int result = 0;
        int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = this.getResources().getDimensionPixelSize(resourceId);
        }
        Log.d("------height", "------height" + result);
        gotoSwitchKeyType();
    }

    private void gotoSwitchKeyType() {
        if (type == 0) {
            image_back.setVisibility(View.GONE);
            passcode_name.setText(getString(R.string.set_passcode));
            passcode_step.setText(getString(R.string.passcode_next));
            passcode_step.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PassCodeActivity.this, PassCodeActivity.class);
                    intent.putExtra(MeValues.KEYCODE_FROM,0);
                    intent.putExtra(MeValues.KEYCODE_TYPE, 1);
                    intent.putExtra(MeValues.FIRST_KEY, inputkey);
                    startActivity(intent);
                    finish();
                }
            });
        } else if (type == 1) {
            image_back.setVisibility(View.GONE);
            passcode_name.setText(getString(R.string.setagain));
            passcode_step.setText(getString(R.string.passcode_goon));
            passcode_step.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String passkey = getIntent().getStringExtra(MeValues.FIRST_KEY);
                    type_from = getIntent().getIntExtra(MeValues.KEYCODE_FROM,0);
                    Log.d("dhn","dhn-fin"+type_from);
                    if (type_from == 0){
                        if (Objects.equals(passkey, inputkey)) {
                            MyTools.setPassword(passkey);
                            Intent intent = new Intent(PassCodeActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(PassCodeActivity.this, getString(R.string.code_not_same), Toast.LENGTH_LONG).show();
                            drawInputView.ClearInputCode();
                            passcode_step.setVisibility(View.GONE);
                        }
                    }else {
                        if (Objects.equals(passkey, inputkey)) {
                            MyTools.setPassword(passkey);
                            Toast.makeText(PassCodeActivity.this, getString(R.string.change_success), Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(PassCodeActivity.this, getString(R.string.code_not_same), Toast.LENGTH_LONG).show();
                            drawInputView.ClearInputCode();
                            passcode_step.setVisibility(View.GONE);
                        }
                    }
                }
            });
        } else if (type == 2) {
            image_back.setVisibility(View.VISIBLE);
            passcode_name.setText(getString(R.string.change_passcode));
            passcode_step.setText(getString(R.string.passcode_change));
            passcode_step.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PassCodeActivity.this, PassCodeActivity.class);
                    intent.putExtra(MeValues.KEYCODE_FROM,2);
                    intent.putExtra(MeValues.KEYCODE_TYPE, 1);
                    intent.putExtra(MeValues.FIRST_KEY, inputkey);
                    startActivity(intent);
                    finish();
                }
            });
            image_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        drawInputView.setInputFinish(new InputCodeFinishListener() {
            @Override
            public void CodeFinish(String code) {
                passcode_step.setVisibility(View.VISIBLE);
                inputkey = code;
            }

            @Override
            public void CodeNotFinish(String code) {
                passcode_step.setVisibility(View.GONE);
                inputkey = code;
            }
        });
    }


    private void initfindview() {
        passcode_main = findViewById(R.id.passcode_main);
        passcode_name = findViewById(R.id.passcode_name);
        passcode_step = findViewById(R.id.passcode_step);
        drawInputView = findViewById(R.id.passcode_draw_input);
        image_back = findViewById(R.id.passcode_back);
    }

    private void showMax(ActionListener listener) {
        MaxManager.ShowAd(PassCodeActivity.this, new onAdStatusListener() {
            @Override
            public void onAdStatus(int type) {
                if (type == MaxManager.type_show_close || type == MaxManager.type_show_fail || type == MaxManager.type_no_cache) {
                    if (listener != null)
                        listener.onAction();
                }
            }
        });
    }
}