package com.xplora.xpsettings.Activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemProperties;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.xplora.xpsettings.R;

public class BlackDoorPasswordActivity extends BaseActionActivity {
    public String mInput = "";
    private TextView mTextView1 = null;
    private TextView mTextView2 = null;
    private TextView mTextView3 = null;
    private TextView mTextView4 = null;
    View.OnClickListener mOnClickListener = v -> {
        int tag = Integer.parseInt((String) v.getTag());
        if (tag == 100) {
            actionBack();
        } else if (tag == 101) {
            actionOK();
        } else {
            if (mInput.length() >= 4)
                return;
            mInput += tag;
            showPinCode();
        }
    };
    private final int subID = 0;
    private final String mSavePin = "";
    private final int mPinOrPuk = 1;  //1:pin 2:puk

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blackdoor_password);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mTextView1 = findViewById(R.id.text1);
        mTextView2 = findViewById(R.id.text2);
        mTextView3 = findViewById(R.id.text3);
        mTextView4 = findViewById(R.id.text4);

        Button button1 = findViewById(R.id.button1);
        Button button2 = findViewById(R.id.button2);
        Button button3 = findViewById(R.id.button3);
        Button button4 = findViewById(R.id.button4);
        Button button5 = findViewById(R.id.button5);
        Button button6 = findViewById(R.id.button6);
        Button button7 = findViewById(R.id.button7);
        Button button8 = findViewById(R.id.button8);
        Button button9 = findViewById(R.id.button9);
        Button button0 = findViewById(R.id.button0);
        ImageButton buttonOK = findViewById(R.id.buttonOK);
        ImageButton buttonBack = findViewById(R.id.buttonBack);

        button1.setOnClickListener(mOnClickListener);
        button2.setOnClickListener(mOnClickListener);
        button3.setOnClickListener(mOnClickListener);
        button4.setOnClickListener(mOnClickListener);
        button5.setOnClickListener(mOnClickListener);
        button6.setOnClickListener(mOnClickListener);
        button7.setOnClickListener(mOnClickListener);
        button8.setOnClickListener(mOnClickListener);
        button9.setOnClickListener(mOnClickListener);
        button0.setOnClickListener(mOnClickListener);
        buttonOK.setOnClickListener(mOnClickListener);
        buttonBack.setOnClickListener(mOnClickListener);
    }

    private void showPinCode() {
        mTextView1.setText(mInput.length() >= 1 ? String.valueOf(mInput.charAt(0)) : "");
        mTextView2.setText(mInput.length() >= 2 ? String.valueOf(mInput.charAt(1)) : "");
        mTextView3.setText(mInput.length() >= 3 ? String.valueOf(mInput.charAt(2)) : "");
        mTextView4.setText(mInput.length() >= 4 ? String.valueOf(mInput.charAt(3)) : "");
    }

    private void actionBack() {
        if (mInput.length() <= 0)
            return;
        mInput = mInput.substring(0, mInput.length() - 1);
        showPinCode();
    }

    private static final String DICTIONARY = "5127940836";
    // type 0: test tool
    // type 1: log tool
    private String getPwd(int type) {
        String imei = SystemProperties.get("persist.zs.imei.number", "");
        if (imei.isEmpty()) {
            if (type == 0)
                return "1123";
            else
                return "2234";
        }
        StringBuilder sb = new StringBuilder();
        String imei_sub;
        if (type == 0) {
            imei_sub = imei.substring(4, 12);
        } else {
            imei_sub = imei.substring(6, 14);
        }
        for (int i = 0; i < imei.length(); i = i + 2) {
            int index = Integer.parseInt(String.valueOf(imei_sub.charAt(i)));
            sb.append(DICTIONARY.charAt(index));
            if (sb.length() == 4) break;
        }
        return sb.toString();
    }

    private String getBackBackDoor() {
        String imei = SystemProperties.get("persist.zs.imei.number", "");
        if (imei.isEmpty()) {
            return "1123";
        }
        String sub = imei.substring(imei.length() - 4);
        StringBuilder sb = new StringBuilder(sub).reverse();
        return sb.toString();
    }

    private void actionOK() {
        int entry = getIntent().getIntExtra("from", 1);
        String pwdTestTool = getPwd(0);
        String pwdLogTool = getPwd(1);
        String pwdBackBackDoor = getBackBackDoor();
        if ((mInput.equals(pwdTestTool) && entry == 0) || mInput.equals(pwdBackBackDoor)) {
            Intent intent = new Intent(BlackDoorPasswordActivity.this, TestToolsActivity.class);
            startActivity(intent);
            finish();
        } else if (mInput.equals(pwdLogTool) && entry == 1) {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(new ComponentName(
                    "com.sikey.commonservice",
                    "com.sikey.commonservice.ui.activity.DiagnosticToolActivity"
            ));
            startActivity(intent);
            finish();
        } else {
            Toast toast = Toast.makeText(this, getString(R.string.blackdoor_password_error), Toast.LENGTH_SHORT);
            toast.show();
            mInput = "";
            showPinCode();
        }
    }
}