package kr.co.vividnext.sodalive.mypage.auth

import android.app.Activity
import android.content.Context
import android.widget.Toast
import com.orhanobut.logger.Logger
import kr.co.bootpay.android.Bootpay
import kr.co.bootpay.android.events.BootpayEventListener
import kr.co.bootpay.android.models.Payload
import kr.co.vividnext.sodalive.BuildConfig
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import java.util.Date

object Auth {
    fun auth(activity: Activity, context: Context, verifyAuth: (String) -> Unit) {
        val authenticationId = "soda_${SharedPreferenceManager.userId}_" +
            "${SharedPreferenceManager.nickname}_${Date().time}"

        val payload = Payload()
            .setApplicationId(BuildConfig.BOOTPAY_APP_ID)
            .setOrderName("본인인증")
            .setPg("다날")
            .setMethod("본인인증")
            .setAuthenticationId(authenticationId)
            .setPrice(0.toDouble())

        Bootpay.init(activity, context)
            .setPayload(payload)
            .setEventListener(object : BootpayEventListener {
                override fun onCancel(data: String) {
                    Logger.e("onCancel: $data")
                }

                override fun onError(data: String) {
                    Logger.e("onError: $data")
                    Toast.makeText(activity, data, Toast.LENGTH_LONG).show()
                }

                override fun onClose() {
                    Logger.e("onClose")
                    Bootpay.removePaymentWindow()
                }

                override fun onIssued(data: String) {
                    Logger.e("onIssued: $data")
                }

                override fun onConfirm(data: String): Boolean {
                    Logger.e("onConfirm: $data")
                    return true
                }

                override fun onDone(data: String) {
                    Logger.e("onDone: $data")
                    verifyAuth(data)
                    Bootpay.removePaymentWindow()
                }
            }).requestAuthentication()
    }
}
