package com.tencent.liteav.demo.player

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.content.Intent
import android.view.View
import androidx.activity.result.contract.ActivityResultContract
import androidx.appcompat.app.AppCompatActivity
import com.tencent.liteav.demo.player.PlayerVerify
import com.tencent.liteav.demo.player.databinding.FragmentVerifyBinding
import java.util.*

class PlayerVerify : AppCompatActivity(), View.OnClickListener {
    private var _binding: FragmentVerifyBinding? = null
    private val binding get() = _binding!!

    private var a: Int = 0
    private var b: Int = 0
    private var c: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = FragmentVerifyBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val random = Random()
        a = random.nextInt(10)
        b = random.nextInt(10)
        c = random.nextInt(10)
        binding.firstNum.text = a.toString()
        binding.secondNum.text = b.toString()
        binding.thirdNum.text = c.toString()
        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)

        setResult(RESULT_CANCELED)
    }

    override fun onClick(v: View) {
        val id = v.id
        if (id == binding.verifyClose.id) {
            finish()
        } else if (id == binding.verifyCheck.id) {
            checkResult()
        } else if (id == binding.verifyBtn0.id) {
            addNumber(0)
        } else if (id == binding.verifyBtn1.id) {
            addNumber(1)
        } else if (id == binding.verifyBtn2.id) {
            addNumber(2)
        } else if (id == binding.verifyBtn3.id) {
            addNumber(3)
        } else if (id == binding.verifyBtn4.id) {
            addNumber(4)
        } else if (id == binding.verifyBtn5.id) {
            addNumber(5)
        } else if (id == binding.verifyBtn6.id) {
            addNumber(6)
        } else if (id == binding.verifyBtn7.id) {
            addNumber(7)
        } else if (id == binding.verifyBtn8.id) {
            addNumber(8)
        } else if (id == binding.verifyBtn9.id) {
            addNumber(9)
        }
    }

    private fun addNumber(num: Int) {
        resetInput()
        val result = binding.resultNum.text.toString()
        if (result.length == 2) {
            checkError()
        } else {
            binding.resultNum.text = result + num
        }
    }

    private fun checkResult() {
        val result = binding.resultNum.text.toString()
        if (!TextUtils.isEmpty(result)) {
            val resultNum = Integer.valueOf(result)
            if (resultNum == a * b + c) {
                setResult(RESULT_OK)
                finish()
            } else {
                checkError()
            }
        }
    }

    private fun checkError() {
        binding.verifyErrorImg.visibility = View.VISIBLE
        binding.resultNum.visibility = View.GONE
        binding.resultNum.text = ""
    }

    private fun resetInput() {
        binding.verifyErrorImg.visibility = View.GONE
        binding.resultNum.visibility = View.VISIBLE
    }

    companion object {
        fun start(activity: AppCompatActivity, requestCode: Int) {
            val intent = Intent(activity, PlayerVerify::class.java)
            activity.startActivityForResult(intent, requestCode)
        }
    }
}