package com.webclip.base;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.TextViewCompat;

import com.webclip.base.databinding.ActivityStartBinding;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class StartActivity extends AppCompatActivity {
    
    ActivityStartBinding activityStartBinding;
    private TextView password1;
    private TextView password2;
    private TextView password3;
    private TextView password4;
    private TextView password5;
    private TextView password6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setNavigationBarColor(getColor(R.color.white));
        super.onCreate(savedInstanceState);
        View decor = getWindow().getDecorView();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        activityStartBinding = ActivityStartBinding.inflate(getLayoutInflater());
        setContentView(activityStartBinding.getRoot());
        setListener();
    }

    public void setListener() {

        password1 = activityStartBinding.password1;
        password2 = activityStartBinding.password2;
        password3 = activityStartBinding.password3;
        password4 = activityStartBinding.password4;
        password5 = activityStartBinding.password5;
        password6 = activityStartBinding.password6;
        activityStartBinding.btn0.setOnClickListener(view -> addInput("0", 0));
        activityStartBinding.btn1.setOnClickListener(view -> addInput("1", 0));
        activityStartBinding.btn2.setOnClickListener(view -> addInput("2", 0));
        activityStartBinding.btn3.setOnClickListener(view -> addInput("3", 0));
        activityStartBinding.btn4.setOnClickListener(view -> addInput("4", 0));
        activityStartBinding.btn5.setOnClickListener(view -> addInput("5", 0));
        activityStartBinding.btn6.setOnClickListener(view -> addInput("6", 0));
        activityStartBinding.btn7.setOnClickListener(view -> addInput("7", 0));
        activityStartBinding.btn8.setOnClickListener(view -> addInput("8", 0));
        activityStartBinding.btn9.setOnClickListener(view -> addInput("9", 0));
        activityStartBinding.btnEqual.setOnClickListener(view -> deleteInput());
    }

    String password = "";

    /**
     * 0 正常增加 1 删除 2 更新
     *
     * @param string
     * @param type
     */
    public void addInput(String string, int type) {
        if (type == 0) {
            if (password.length() < 6) {
                password += string;
            }
        } else if (type == 1) {
            password = password.substring(0, password.length() - 1);
        }
        if (password.length() == 6) {
            password6.setText(password.charAt(5) + "");
            toNext();
        } else {
            password6.setText("");
            if (password.length() == 5) {
                password5.setText(password.charAt(4) + "");
            } else {
                password5.setText("");
                if (password.length() == 4) {
                    password4.setText(password.charAt(3) + "");
                } else {
                    password4.setText("");
                    if (password.length() == 3) {
                        password3.setText(password.charAt(2) + "");
                    } else {
                        password3.setText("");
                        if (password.length() == 2) {
                            password2.setText(password.charAt(1) + "");
                        } else {
                            password2.setText("");
                            if (password.length() == 1) {
                                password1.setText(password.charAt(0) + "");
                            } else {
                                password1.setText("");
                            }
                        }
                    }
                }
            }
        }
    }



    private void toNext() {
        MainActivity.saveString(StartActivity.this, "loc_pass", password);
        Intent intent = new Intent(StartActivity.this, IndexActivity.class);
        startActivity(intent);
        finish();

    }



    private void deleteInput() {
        if (TextUtils.isEmpty(password)) {
            addInput("", 2);
        } else {
            addInput(password, 1);
        }
    }

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