package com.zswc.creativity.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.qmuiteam.qmui.kotlin.onClick
import com.zswc.creativity.base.MBActivity
import com.zswc.creativity.databinding.ActivityErrorBinding
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel


/**
 *Created by shihuiyun
 *on 2020/11/10
 */
class CustomErrorActivity : MBActivity<BViewModel<BRepository>, ActivityErrorBinding>(){


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        var config: CaocConfig? =null
        try {
            binding.errorDetails.text=CustomActivityOnCrash.getStackTraceFromIntent(intent)
             config = CustomActivityOnCrash.getConfigFromIntent(intent)

        }catch (e:Exception){
        }

        binding.restartCopy.onClick {
            //复制错误信息

            val cm: ClipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            // 创建普通字符型ClipData
            val mClipData = ClipData.newPlainText(null, binding.errorDetails.text)
            // 将ClipData内容放到系统剪贴板里。
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData)
            toast("错误信息已复制到粘贴板")
        }


        if (config == null) {
            finish()
            return
        }

        if (config.isShowRestartButton && config.restartActivityClass != null) {
            binding.restartButton.text="重启应用"

            binding.restartButton.onClick {
                CustomActivityOnCrash.restartApplication(this@CustomErrorActivity, config)
            }


        } else {

            binding.restartButton.onClick {
                CustomActivityOnCrash.closeApplication(this@CustomErrorActivity, config)
            }

        }


    }


    override fun title(): String? {
        return "错误信息"

    }

    override fun binding(): ActivityErrorBinding {
        return ActivityErrorBinding.inflate(layoutInflater)
    }
}
