package uz.myid.sdk.sample

import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import uz.myid.android.sdk.capture.*
import uz.myid.android.sdk.capture.model.OrganizationDetails
import uz.myid.sdk.sample.databinding.ActivityMainBinding
import java.util.*

class MainActivity : AppCompatActivity(), MyIdResultListener {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    private val myIdClient = MyIdClient()

    private var clientId = ""
    private var myIdBuildMode = MyIdBuildMode.PRODUCTION
    private var myIdEntryType = MyIdEntryType.AUTH
    private var myIdLocale = Locale("en")
    private var myIdCameraShape = MyIdCameraShape.CIRCLE

    private var code = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        with(binding) {
            inputClientId.setText(clientId)

            radioGroupLang.setOnCheckedChangeListener { _, checkedId ->
                myIdLocale = when (checkedId) {
                    R.id.radioUz -> Locale("uz")
                    R.id.radioEn -> Locale("en")
                    else -> Locale("ru")
                }
            }
            radioGroupEntryType.setOnCheckedChangeListener { _, checkedId ->
                myIdEntryType = if (checkedId == R.id.radioFace) {
                    MyIdEntryType.FACE
                } else {
                    MyIdEntryType.AUTH
                }
            }
            radioGroupBuildMode.setOnCheckedChangeListener { _, checkedId ->
                myIdBuildMode = if (checkedId == R.id.radioProd) {
                    MyIdBuildMode.PRODUCTION
                } else {
                    MyIdBuildMode.DEBUG
                }
            }
            radioGroupShape.setOnCheckedChangeListener { _, checkedId ->
                myIdCameraShape = if (checkedId == R.id.radioCircle) {
                    MyIdCameraShape.CIRCLE
                } else {
                    MyIdCameraShape.ELLIPSE
                }
            }

            buttonStart.setOnClickListener { startMyId() }
        }
    }

    override fun onSuccess(result: MyIdResult) {
        code = result.code.orEmpty()

        with(binding) {
            imageResult.setImageBitmap(result.bitmap)
            """
                Result code: ${result.code}
                Comparison value: ${result.comparison}
            """.trimIndent().also { textResult.text = it }
        }
    }

    override fun onError(e: MyIdException) {
        code = ""

        with(binding) {
            imageResult.setImageBitmap(null)
            """
                Result error: ${e.message}
                Result error code: ${e.code}
            """.trimIndent().also { textResult.text = it }
        }
    }

    override fun onUserExited() {
        code = ""

        with(binding) {
            imageResult.setImageBitmap(null)
            "User exited sdk".also { textResult.text = it }
        }
    }

    private fun startMyId() {
        val organizationDetails = OrganizationDetails(
            phoneNumber = binding.inputPhoneNumber.value
        )

        val myIdConfig = MyIdConfig.builder(binding.inputClientId.value)
            .withPassportData(binding.inputPassportData.value)
            .withBirthDate(binding.inputDate.value)
            .withSdkHash(binding.inputSdkHash.value)
            .withExternalId(binding.inputExternalId.value)
            .withThreshold(binding.thresholdSlider.value)
            .withBuildMode(myIdBuildMode)
            .withEntryType(myIdEntryType)
            .withLocale(myIdLocale)
            .withCameraShape(myIdCameraShape)
            .withOrganizationDetails(organizationDetails)
            .withPhoto(binding.checkboxWithPhoto.isChecked)
            .build()

        val intent = myIdClient.createIntent(this, myIdConfig)
        result.launch(intent)
    }

    private val result = takeUserResult(this)

    private inline val EditText.value: String get() = text.toString().trim()
}