package com.yyxx.commsdk.demo

import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.InputType
import android.view.KeyEvent

import android.view.View
import android.widget.*
import cn.yyxx.commsdk.base.YYXXCommSdk
import cn.yyxx.commsdk.base.constants.Constants
import cn.yyxx.commsdk.base.entity.ResultInfo
import cn.yyxx.commsdk.base.entity.SdkChargeInfo
import cn.yyxx.commsdk.base.entity.SdkRoleInfo
import cn.yyxx.commsdk.base.internal.ICallback


import cn.yyxx.support.hawkeye.ToastUtils
import cn.yyxx.support.permission.Permission
import org.json.JSONObject


/**
 * @author #Suyghur.
 * Created on 2021/12/26
 */
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 定额充值"),
        Item(7, "07 权限申请测试"),
        Item(8, "08 上报游戏事件"),
    )

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

    private var cacheRoleInfo: SdkRoleInfo? = 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()

        YYXXCommSdk.getInstance().initialize(this,object: ICallback{
            override fun onResult(code: Int, result: String?) {

                ToastUtils.toastInfo(this@DemoActivity,"code:$code   msg:$result")
            }
        })

        YYXXCommSdk.getInstance().setLogoutListener(this,object :ICallback{
            override fun onResult(code: Int, result: String?) {
                if (code == ResultInfo.CODE_SUCCESS){
                    ToastUtils.toastInfo(this@DemoActivity, "退出成功，执行游戏注销")

                }
            }
        })

        YYXXCommSdk.getInstance().onCreate(this,savedInstanceState)

    }



    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)
                if (id == 5) {
                    mEditText = EditText(this@DemoActivity)
                    mEditText.hint = "请输入金额，单位分"
                    mEditText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_CLASS_PHONE
                    mEditText.setText("100")
                    this@DemoActivity.layout.addView(mEditText)
                }
            }
        }
    }


    override fun onClick(v: View?) {
        v?.apply {
            when (tag as Int) {
                0 -> {

                }
                1 -> {
                     YYXXCommSdk.getInstance().login(this@DemoActivity){code,result->
                         if (code == ResultInfo.CODE_SUCCESS){
                            ToastUtils.toastInfo(this@DemoActivity,"code:$code   msg:$result")
                         }else{
                             ToastUtils.toastInfo(this@DemoActivity,"code:$code   msg:$result")

                         }
                     }
                }
                2 -> {
                    YYXXCommSdk.getInstance().logout()
                }
                3 -> {
                    YYXXCommSdk.getInstance().uploadRoleInfo(this@DemoActivity,Constants.UPLOAD_TYPE_CREATE_ROLE,getGameRoleInfo())
                }
                4 -> {
                    YYXXCommSdk.getInstance().uploadRoleInfo(this@DemoActivity,Constants.UPLOAD_TYPE_ENTER_GAME,getGameRoleInfo())

                }
                5 -> {
                    YYXXCommSdk.getInstance().uploadRoleInfo(this@DemoActivity,Constants.UPLOAD_TYPE_LEVEL_UPGRADE,getGameRoleInfo())

                }
                6 -> {

                    if (cacheRoleInfo == null) {
                        ToastUtils.toastInfo(this@DemoActivity, "请先创建/登录角色")
                        return
                    }

                    YYXXCommSdk.getInstance().charge(this@DemoActivity,getGameChargeRoleInfo(),{code,result->
                        if (code == ResultInfo.CODE_SUCCESS){

                        }
                    })
                }
                7 -> {
                    YYXXCommSdk.getInstance().requestPermission(this@DemoActivity, mutableListOf(Permission.CAMERA)){code,result->
                        ToastUtils.toastInfo(this@DemoActivity, "requestPermission code: $code, result: $result")

                    }
                }
                8 ->{
                    YYXXCommSdk.getInstance().reportGameEvent("主线任务1", getGameEvent());
                }

            }
        }



    }



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

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

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

    override fun onPause() {
        super.onPause()
        YYXXCommSdk.getInstance().onPause(this)
    }
    override fun onStop() {
        super.onStop()
        YYXXCommSdk.getInstance().onStop(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        YYXXCommSdk.getInstance().onDestroy(this)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        YYXXCommSdk.getInstance().onRequestPermissionsResult(requestCode,permissions,grantResults)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        YYXXCommSdk.getInstance().onConfigurationChanged(this,newConfig)
    }
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        YYXXCommSdk.getInstance().onWindowFocusChanged(this,hasFocus)
    }

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

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


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            YYXXCommSdk.getInstance().openExitView(this,object :ICallback{
                override fun onResult(code: Int, result: String?) {
                    if (code == ResultInfo.CODE_SUCCESS){
                        if (YYXXCommSdk.getInstance().hasExitView(this@DemoActivity)){

                            finish()


                        }else{

                            val msg = "是否关闭游戏？"
                            AlertDialog.Builder(this@DemoActivity)
                                .setMessage(msg)
                                .setTitle("温馨提示")
                                .setPositiveButton("确定",DialogInterface.OnClickListener(){_,_->
                                    finish()
                                })
                                .setNegativeButton("取消",null)
                                .create()
                                .show()
                        }

                    }


                }
            })
            return true

        }
        return super.onKeyDown(keyCode, event)
    }
    private fun getGameRoleInfo(): SdkRoleInfo {
        val roleInfo = SdkRoleInfo()
        roleInfo.serverId = "102201"
        roleInfo.serverName = "霸天1服"
        roleInfo.roleId ="101"
        roleInfo.roleName = "虚空"
        roleInfo.roleLevel = "100"
        roleInfo.gender = "男"
        roleInfo.hasGold  = "100"
        roleInfo.charge = "10"
        roleInfo.vipLevel = "1"
        roleInfo.partyId  = "3399"
        roleInfo.partyName = "子湖"
        roleInfo.reincarnationCount = "0"
        roleInfo.rolePower = "101839303844302"
        roleInfo.profession = "法师"

        var  time = System.currentTimeMillis() / 1000
        roleInfo.roleCreateTime ="$time"

        cacheRoleInfo = roleInfo
        return roleInfo
    }


    public fun getGameEvent():JSONObject{
        var json = JSONObject();
        json.put("坐骑支线","坐骑获取成功")
        json.put("武器支线","武器获取成功")
        return  json
    }
    private fun getGameChargeRoleInfo():SdkChargeInfo{
        val chargeInfo = SdkChargeInfo()
        var  time = System.currentTimeMillis() / 1000
        chargeInfo.count = "10"
        chargeInfo.ext = "$time"
        chargeInfo.game_no = "$time"
        chargeInfo.count = "10"
        chargeInfo.hasGold = "80"
        chargeInfo.order_desc = "购买10元宝"
        chargeInfo.product_name = "购买10元宝"
        chargeInfo.party_name = "霸天"
        chargeInfo.party_id = "11"
        chargeInfo.product_id = "299"
        chargeInfo.pay_money = "100"
        chargeInfo.radio = "10"
        chargeInfo.reincarnationCount = "0"
        chargeInfo.product_name = "元宝"
        chargeInfo.role_id = "133"
        chargeInfo.role_level = "30"
        chargeInfo.role_name = "可爱小呆瓜"
        chargeInfo.server_id = "100"
        chargeInfo.server_name = "惊天1服"
        chargeInfo.vip_level = "0"

        return chargeInfo
    }

}