package com.eyuangame.demo

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.KeyEvent
import android.view.View
import android.widget.*
import cn.yyxx.eyuangame.base.EYuanGame
import cn.yyxx.eyuangame.base.entity.SdkChargeInfo
import cn.yyxx.eyuangame.base.entity.SdkEvent
import cn.yyxx.eyuangame.base.entity.SdkRoleInfo
import cn.yyxx.eyuangame.base.internal.ICallback
import cn.yyxx.support.hawkeye.LogUtils
import cn.yyxx.support.hawkeye.ToastUtils
import kotlin.system.exitProcess


/**
 * @author #Suyghur.
 * Created on 2021/06/09
 */
class DemoActivity : Activity(), View.OnClickListener {

    private val events = arrayListOf(
        Item(0, "00 接口环境切换"),
        Item(1, "01 登录"),
        Item(2, "02 切换账号"),
        Item(3, "03 角色创建上报"),
        Item(4, "04 角色登录上报"),
        Item(5, "05 角色升级上报"),
        Item(6, "06 Google应用内购"),
        Item(7, "07 第三方H5支付"),
        Item(8, "08 Crashlytics崩溃测试"),
        Item(9, "09 模拟CP打点（玩家首次完成新手引导）"),
        Item(10, "10 模拟CP打点（玩家首次完成结缘）")
    )


    private lateinit var layout: LinearLayout
    private lateinit var mTextView: TextView

    private var cacheRoleInfo: CacheRoleInfo.RoleInfo? = null

    private val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                10001 -> {
                    with(mTextView) {
                        text = text.toString() + msg.obj.toString()
                    }
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        LogUtils.handler = handler
        EYuanGame.getInstance().initialize(this, false, object : ICallback {
            override fun onResult(code: Int, result: String) {
                ToastUtils.toastInfo(this@DemoActivity, "---- demo提示不做翻译 ----\ncode : $code\n msg : $result\n ---- demo提示不做翻译 ----")
            }
        })
    }

    private fun initView() {
        layout = LinearLayout(this)
        layout.orientation = LinearLayout.VERTICAL
        initButton()
        mTextView = TextView(this)
        with(mTextView) {
            text = ""
            this@DemoActivity.layout.addView(this)
        }
        val scrollView = ScrollView(this)
        scrollView.addView(layout)
        setContentView(scrollView)
    }

    private fun initButton() {
        for (event in events) {
            with(Button(this)) {
                text = event.name
                tag = event.id
                id = event.id
                setOnClickListener(this@DemoActivity)
                this@DemoActivity.layout.addView(this)
            }
        }
    }

    override fun onClick(v: View?) {
        v?.apply {
            mTextView.text = ""
            when (tag as Int) {
                0 -> EnvActivity.start(this@DemoActivity)
                1 -> {
                    EYuanGame.getInstance().login(this@DemoActivity, true, object : ICallback {
                        override fun onResult(code: Int, result: String) {
                            ToastUtils.toastInfo(this@DemoActivity, "---- demo提示不做翻译 ----\ncode : $code\n msg : $result\n ---- demo提示不做翻译 ----")
                            if (code == 0) {
                                cacheRoleInfo = CacheRoleInfo.getDemoRoleInfo(this@DemoActivity, EYuanGame.getInstance().getCurrentUserId())
                            }
                        }
                    })
                }
                2 -> {
                    EYuanGame.getInstance().logout(this@DemoActivity, object : ICallback {
                        override fun onResult(code: Int, result: String) {
                            if (code == 0) {
                                EYuanGame.getInstance().login(this@DemoActivity, false, object : ICallback {
                                    override fun onResult(code: Int, result: String) {
                                        ToastUtils.toastInfo(this@DemoActivity, "---- demo提示不做翻译 ----\ncode : $code\n msg : $result\n ---- demo提示不做翻译 ----")
                                    }
                                })
                            }
                        }
                    })
                }
                3 -> {
                    cacheRoleInfo = null
                    cacheRoleInfo = CacheRoleInfo.setDemoRoleInfo(this@DemoActivity, EYuanGame.getInstance().getCurrentUserId())
                    EYuanGame.getInstance().roleCreate(this@DemoActivity, getGameRoleInfo())
                }
                4 -> EYuanGame.getInstance().roleLauncher(this@DemoActivity, getGameRoleInfo())
                5 -> EYuanGame.getInstance().roleLevelUp(this@DemoActivity, getGameRoleInfo())
                6 -> {
                    EYuanGame.getInstance().charge(this@DemoActivity, getGameChargeInfo(), true, object : ICallback {
                        override fun onResult(code: Int, result: String) {
                            ToastUtils.toastInfo(this@DemoActivity, "---- demo提示不做翻译 ----\ncode : $code\n msg : $result\n ---- demo提示不做翻译 ----")
                        }

                    })
                }
                7 -> {
                    EYuanGame.getInstance().charge(this@DemoActivity, getGameChargeInfo(), false, object : ICallback {
                        override fun onResult(code: Int, result: String) {
                            ToastUtils.toastInfo(this@DemoActivity, "---- demo提示不做翻译 ----\ncode : $code\n msg : $result\n ---- demo提示不做翻译 ----")
                        }

                    })
                }
                8 -> throw RuntimeException("Test Crashlytics Feature")
                9 -> {
                    val sdkEvent = SdkEvent()
                    sdkEvent.eventName = "tutorial"
                    sdkEvent.standard = true
                    sdkEvent.fbAliasName = "fb_mobile_tutorial_completion"
                    EYuanGame.getInstance().linkingEvent(this@DemoActivity, sdkEvent)
                }
                10 -> {
                    val sdkEvent = SdkEvent()
                    sdkEvent.eventName = "finish_marry"
                    sdkEvent.standard = false
                    EYuanGame.getInstance().linkingEvent(this@DemoActivity, sdkEvent)
                }
            }
        }
    }

    private fun getGameRoleInfo(): SdkRoleInfo {
        val gameRoleInfo = SdkRoleInfo()
        //用户ID
        gameRoleInfo.userId = EYuanGame.getInstance().getCurrentUserId()
        cacheRoleInfo?.apply {
            //角色ID
            gameRoleInfo.roleId = roleId
            //角色名称
            gameRoleInfo.roleName = roleName
            //角色等级
            gameRoleInfo.roleLevel = roleLevel
            gameRoleInfo.roleCTime = roleCTime
            //服务器ID
            gameRoleInfo.serverId = serverCode
            //服务器名
            gameRoleInfo.serverName = serverName
            //用户VIP等级，无该字段则传空串""
            gameRoleInfo.vipLevel = vipLevel
            //当前角色游戏币余额
            gameRoleInfo.balance = balance
            gameRoleInfo.totalPurchase = totalPurchase
        }
        return gameRoleInfo
    }

    private fun getGameChargeInfo(): SdkChargeInfo {
        val gameChargeInfo = SdkChargeInfo()
        val orderId = "order_" + System.currentTimeMillis()
        //用户ID
        gameChargeInfo.userId = EYuanGame.getInstance().getCurrentUserId()
        cacheRoleInfo?.apply {
            //角色ID
            gameChargeInfo.roleId = roleId
            //角色名称
            gameChargeInfo.roleName = roleName
            //角色等级
            gameChargeInfo.roleLevel = roleLevel
            //创角时间戳
            gameChargeInfo.roleCTime = roleCTime
            //服务器ID
            gameChargeInfo.serverId = serverCode
            //服务器名
            gameChargeInfo.serverName = serverName
        }
        //游戏的订单号
        gameChargeInfo.cpOrderId = orderId
        //透传字段，会在回调地址中原样返回
        gameChargeInfo.cpExt = "cp_callback_info||$orderId"
        //金额，单位分，币种美金
        gameChargeInfo.amount = 99
        //商品ID，计费点
        gameChargeInfo.productId = "com.shzd.1usd"
        //商品名称
        gameChargeInfo.productName = "60元寶"
        //商品描述
        gameChargeInfo.productDesc = "60元寶"
        gameChargeInfo.cpPayTime = System.currentTimeMillis() / 1000
        gameChargeInfo.cpPaySign = "test123"
        return gameChargeInfo
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            EYuanGame.getInstance().openExitView(this, object : ICallback {
                override fun onResult(code: Int, result: String) {
                    if (code == 0) {
                        finish()
                    }
                }
            })
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onStart() {
        super.onStart()
        EYuanGame.getInstance().onStart(this)
    }

    override fun onResume() {
        super.onResume()
        EYuanGame.getInstance().onResume(this)
    }

    override fun onRestart() {
        super.onRestart()
        EYuanGame.getInstance().onRestart(this)
    }

    override fun onPause() {
        super.onPause()
        EYuanGame.getInstance().onPause(this)
    }

    override fun onStop() {
        super.onStop()
        EYuanGame.getInstance().onStop(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EYuanGame.getInstance().onDestroy(this)
        exitProcess(0)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        EYuanGame.getInstance().onActivityResult(this, requestCode, resultCode, data)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        EYuanGame.getInstance().onNewIntent(this, intent)
    }

}