package com.xplora.xpsettings.Activity;

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

import com.xplora.xpsettings.R;

public class BlackDoorActivity extends BasePasswordActivity {
    private RelativeLayout mLockLayout = null;
    private TextView mTextView1 = null;
    public String mInput = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blackdoor_password);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        resetAfterTimeout(false);
        initInputView();
        refreshLockView();
        getImei();
    }

    private void refreshLockView() {
        if (!isLock())
            return;
        long minutes = getLeftTime() / 60;
        long second = getLeftTime() % 60;
        long time = minutes + (second > 0 ? 1 : 0);
        String content = getString(R.string.bd_3_content, time);

        mLockLayout = findViewById(R.id.lockLayout);
        TextView errorContent = findViewById(R.id.mErrorContent);
        Button button = findViewById(R.id.mErrorBtn);

        mLockLayout.setVisibility(View.VISIBLE);
        errorContent.setText(content);
        button.setOnClickListener((e)-> {
            finish();
        });
    }

    private void initInputView() {
        mTextView1 = findViewById(R.id.text1);
        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);
    }

    View.OnClickListener mOnClickListener = v -> {
        int tag = Integer.parseInt((String) v.getTag());
        if (tag == 100) {
            actionBack();
        } else if (tag == 101) {
            actionOK();
        } else {
//            if (mInput.length() >= 6)
//                return;
            mInput += tag;
            showPinCode();
        }
    };

    private void showPinCode() {
        mTextView1.setText(mInput);
    }

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

    private void actionOK() {
        int entry = getIntent().getIntExtra("from", 1);
        if (entry == 0 && (checkTestPassword (mInput) || checkDefaultPassword(mInput))) {
            resetAfterTimeout(true);
            Intent intent = new Intent(BlackDoorActivity.this, TestToolsActivity.class);
            startActivity(intent);
            finish();
        } else if (entry == 1 && (checkLogPassword(mInput) || checkDefaultPassword(mInput))) {
            resetAfterTimeout(true);
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(new ComponentName(
                    "com.xplora.commonservice",
                    "com.xplora.commonservice.ui.activity.DiagnosticToolActivity"
            ));
            startActivity(intent);
            finish();
        } else {
            mInput = "";
            showPinCode();
            addError();
            refreshLockView();
            Toast toast = Toast.makeText(this, getString(R.string.blackdoor_password_error), Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}