package com.yyxxgame.columbus

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.internal.ICallback
import cn.yyxx.eyuangame.base.utils.Logger
import cn.yyxx.eyuangame.core.entity.FloatFeature
import cn.yyxx.eyuangame.core.impl.SdkDrive
import cn.yyxx.eyuangame.core.impl.floating.FloatCenterItem
import cn.yyxx.support.hawkeye.LogUtils
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 定额充值"),
    )


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

    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) {
            }
        })
    }

    private fun initView() {
        layout = LinearLayout(this)
        layout.orientation = LinearLayout.VERTICAL
        initButton()
        mTextView = TextView(this)
        with(mTextView) {
            text = ""
            this@DemoActivity.layout.addView(this)
        }
        val itemView = FloatCenterItem(this, "", FloatFeature.FEATURE_MEMBER)
        layout.addView(itemView)
        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) {

                        }
                    })
                }
                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) {
                                        }
                                    })
                            }
                        }
                    })
                }
                3 -> {
                    val result = SdkDrive.instance.test()
                    Logger.d(result)
                }
            }
        }
    }

    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)
    }

}