package com.demo.xinrui

import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import cn.yyxx.support.hawkeye.LogUtils
import cn.yyxx.support.hawkeye.ToastUtils
import com.yyrh.core.entity.Session
import com.yyrh.core.impl.login.LoginActivity
import com.yyrh.sdk.SDKEntry
import com.yyrh.sdk.callback.InitCallback
import com.yyrh.sdk.callback.LoginCallback
import com.yyrh.ui.dialog.TipsToast
import com.yyrh.utils.SessionUtils
import com.yyrh.utils.YYLog

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

    private val events = arrayListOf(
        Item(1, "01 登录"),
        Item(2, "02 切换账号"),
        Item(3, "03 角色创建上报"),
        Item(4, "04 角色登录上报"),
        Item(5, "05 角色升级上报"),
        Item(6, "06 测试测试"),
        Item(7, "07 测试测试"),
    )

    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
        SDKEntry.getSdkInstance().initSdk(this, object : InitCallback {
            override fun onInitSuccess(msg: String?) {
                ToastUtils.toastInfo(this@DemoActivity, msg)
            }

            override fun onInitFailed(msg: String?) {
                ToastUtils.toastInfo(this@DemoActivity, msg)
            }

            override fun onInitError(msg: String?) {
                ToastUtils.toastInfo(this@DemoActivity, msg)

            }

            override fun onGetOaid(isSuccess: Boolean, oaid: String?) {
                ToastUtils.toastInfo(this@DemoActivity, oaid)
            }

            override fun onShowGameExit() {
            }

        }, true)
    }

    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 onResume() {
        super.onResume()
        SDKEntry.getSdkInstance().sdkOnResume(this)
    }

    override fun onPause() {
        super.onPause()
        SDKEntry.getSdkInstance().sdkOnPause(this)

    }

    override fun onDestroy() {
        super.onDestroy()
        SDKEntry.getSdkInstance().sdkOnDestroy(this)

    }

    override fun onClick(v: View?) {
        v?.apply {
            when (id) {
                1 -> {
                    SDKEntry.getSdkInstance().login(this@DemoActivity, object : LoginCallback {
                        override fun onLoginSuccess(token: String?) {
                            ToastUtils.toastInfo(this@DemoActivity, "登录成功，token: $token")
                        }

                        override fun onLoginFailed(msg: String?) {
                            ToastUtils.toastInfo(this@DemoActivity, "登录失败，msg: $msg")
                        }

                        override fun onLoginError(msg: String?) {
                            ToastUtils.toastInfo(this@DemoActivity, "登录失败，msg: $msg")
                        }

                    })
                }
            }
        }
    }
}