package com.yidian.yac.userinfo.ui

import android.os.Bundle
import android.util.Log
import androidx.core.os.bundleOf
import com.yidian.common.base.BaseFragment
import com.yidian.xarc.xbase.net.CheckUpdateCallback
import com.yidian.yac.userinfo.R
import com.yidian.yac.userinfo.bean.ImageItemBean
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xpage.XPageManager
import com.yidian.yac.pm.appupdate.AppUpdateEchoData
import com.yidian.yac.pm.appupdate.AppUpdateInfo
import com.yidian.yac.pm.appupdate.YDAppUpdateManager
import com.yidian.yac.pm.appupdate.callback.AppUpdateCallback
import com.yidian.yac.pm.base.UpdateInfo
import kotlinx.android.synthetic.main.fragment_.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File

class UserInfoFragment:BaseFragment() {
    companion object{
        fun newInstance(): UserInfoFragment {
            val fragment = UserInfoFragment()
            val args = bundleOf()
            fragment.arguments = args
            return fragment
        }
    }
    override val contentViewId: Int
        get() = R.layout.fragment_

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        EventBus.getDefault().register(this)
        tv_content.text = "我的页面"
        setClickListener()
    }

    private fun setClickListener(){
        circle_image.setOnClickListener{
            imagePopupShow()
        }
        btn_jump.setOnClickListener{
            XPageManager.push("/home", null)
        }

        btn_update.setOnClickListener {
            YDAppUpdateManager.checkAppUpdate(object : CheckUpdateCallback(){
                override fun onFail(msg: String) {
                    Log.d("yue.huang", "checkAppUpdate_onFail:$msg")
                }

                override fun onSucess(updateInfo: UpdateInfo<Any>?) {
                    var update =  updateInfo as AppUpdateInfo<AppUpdateEchoData>
                    if(update.hasUpdate){
                        YDAppUpdateManager.download(update,object : AppUpdateCallback() {
                            override fun onProgress(total: Long, current: Long, percentage: Int) {
                                Log.d("yue.huang", "进度：========$percentage")
                            }

                            override fun onDownloadSuccess(apkFile: File, isCache: Boolean) {
                                YDAppUpdateManager.installApk(apkFile)
                            }

                            override fun onDownloadFailed() {
                            }

                            override fun onDownloadPaused() {
                            }

                            override fun onInstallFailed() {
                            }

                            override fun onSignatureVerifiyFailed() {
                            }
                        })
                    }
                }

            })
        }
    }

    //先执行Main再执行MAIN_ORDERED,priority值越大优先级越高
    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED, priority = 9)
    fun onEvent(event: XBaseEvent) {
        // TODO: 2021/1/14  XEvent 测试用
        println("---->onEvent:${event.name}+${event.body}")
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


    private fun imagePopupShow(){
        val imagePopupWindow = activity?.let { ImagePopupWindow(it) }
        var imageList = ArrayList<ImageItemBean>()
        for (i in 1..5){
            imageList.add(ImageItemBean(R.mipmap.head_icon))
        }
        imagePopupWindow?.updateImageList(imageList)
        imagePopupWindow?.show(lly_root)
    }
}