package com.tencent.liteav.demo.player;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tencent.liteav.demo.player.databinding.ActivityVerifyBinding;

import java.util.Random;

public class PlayerVerify extends AppCompatActivity implements View.OnClickListener {

    public static void start(Context context) {
        Intent intent = new Intent(context, PlayerVerify.class);
        context.startActivity(intent);
    }

    private ActivityVerifyBinding binding;

    private int a = 0, b = 0, c = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityVerifyBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Random random = new Random();
        a = random.nextInt(10);
        b = random.nextInt(10);
        c = random.nextInt(10);

        binding.firstNum.setText(String.valueOf(a));
        binding.secondNum.setText(String.valueOf(b));
        binding.thirdNum.setText(String.valueOf(c));

        binding.verifyBtn0.setOnClickListener(this);
        binding.verifyBtn1.setOnClickListener(this);
        binding.verifyBtn2.setOnClickListener(this);
        binding.verifyBtn3.setOnClickListener(this);
        binding.verifyBtn4.setOnClickListener(this);
        binding.verifyBtn5.setOnClickListener(this);
        binding.verifyBtn6.setOnClickListener(this);
        binding.verifyBtn7.setOnClickListener(this);
        binding.verifyBtn8.setOnClickListener(this);
        binding.verifyBtn9.setOnClickListener(this);

        binding.verifyClose.setOnClickListener(this);
        binding.verifyCheck.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == binding.verifyClose.getId()) {
            finish();
        } else if (id == binding.verifyCheck.getId()) {
            checkResult();
        } else if (id == binding.verifyBtn0.getId()) {
            addNumber(0);
        } else if (id == binding.verifyBtn1.getId()) {
            addNumber(1);
        } else if (id == binding.verifyBtn2.getId()) {
            addNumber(2);
        } else if (id == binding.verifyBtn3.getId()) {
            addNumber(3);
        } else if (id == binding.verifyBtn4.getId()) {
            addNumber(4);
        } else if (id == binding.verifyBtn5.getId()) {
            addNumber(5);
        } else if (id == binding.verifyBtn6.getId()) {
            addNumber(6);
        } else if (id == binding.verifyBtn7.getId()) {
            addNumber(7);
        } else if (id == binding.verifyBtn8.getId()) {
            addNumber(8);
        } else if (id == binding.verifyBtn9.getId()) {
            addNumber(9);
        }
    }

    private void addNumber(int num) {
        resetInput();
        String result = binding.resultNum.getText().toString();

        binding.resultNum.setText(result + num);
    }

    private void checkResult() {
        String result = binding.resultNum.getText().toString();
        if (!TextUtils.isEmpty(result)) {
            int resultNum = Integer.valueOf(result);

            if (resultNum == a * b + c) {
                finish();
            } else {
                checkError();
            }
        }
    }

    private void checkError() {
        binding.verifyErrorImg.setVisibility(View.VISIBLE);
        binding.resultNum.setVisibility(View.GONE);
        binding.resultNum.setText("");
    }

    private void resetInput() {
        binding.verifyErrorImg.setVisibility(View.GONE);
        binding.resultNum.setVisibility(View.VISIBLE);
    }
}
