package com.zswc.creativity.vmodel

import android.app.Application
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat.getSystemService
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.UiSwitch
import com.zswc.creativity.R
import com.zswc.creativity.activity.*
import com.zswc.creativity.model.MyCollection
import com.zswc.creativity.model.User
import com.zswc.creativity.utils.net.NetEngine
import kotlinx.coroutines.launch


/**
 *Created by shihuiyun
 *on 2020/9/23
 */
open class HomeMyVModel(application: Application) : BViewModel<BRepository>(application) {


    var user = MutableLiveData<User>(User.get())

    //true 是登录模式  false 游客模式
     var isLogin=MutableLiveData<Boolean>(true)
    var is_auth=MutableLiveData<Boolean>(true)
    var is_show=MutableLiveData<Boolean>(false)
    //列表数据
    var list=MutableLiveData<List<MyCollection>>()


    fun toLogin(){
        UiSwitch.single(repository().context, LoginActivity::class.java)

    }
    fun toSetting(){
        if(!isLogin.value!!){
            toast("请登录")
            return
        }
        UiSwitch.single(repository().context, SettingActivity::class.java)

    }


    fun isShowAuth(is_auth:Boolean,isLogin:Boolean):Int{
        if(isLogin&&!is_auth){
            return  View.VISIBLE
        }
        return View.GONE
    }

    fun isShowAuth(auth:String):Int{
        if(auth == "0"){
            return R.drawable.ic_renzheng_no

        }
        return R.drawable.ic_renzheng
    }

    fun toMyOrder(){
        if(!isLogin.value!!){
            toast("请登录")
            return
        }
        UiSwitch.single(repository().context, MyOrderActivity::class.java)

    }
    fun toCopyText(){

        val cm: ClipboardManager? = repository().context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
        val mClipData = ClipData.newPlainText("Label", user.value?.wallet_address)
        cm?.setPrimaryClip(mClipData)
        toast("文字已复制到粘贴板")

    }
     fun toIntegral(){
         //积分
         toast("暂未开放，敬请期待")
     }

    fun toConsignmentOrder(){
        if(!isLogin.value!!){
            toast("请登录")
            return
        }
        UiSwitch.single(repository().context, ConsignmentOrderActivity::class.java)


    }

    fun toCertification(){
        if(!isLogin.value!!){
            toast("请登录")
            return
        }
        if(user.value?.is_auth=="1"){//已认证

            UiSwitch.single(repository().context, AuthenActivity::class.java)

        }else{
            UiSwitch.single(repository().context, CertificationActivity::class.java)

        }


    }
  //   去推广
    fun toSpread(){

      if(!isLogin.value!!){
          toast("请登录")
          return
      }
      UiSwitch.single(repository().context, SpreadActivity::class.java)
  }
    fun toMyWallet(){
        if(!isLogin.value!!){
            toast("请登录")
            return
        }
        UiSwitch.single(repository().context, MyWalletActivity::class.java)

    }


    fun getUserInfo(){

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getUser() }
            if(it.ok()) {
                it.data()?.let { user ->
                    user.save()
                }
                user.value = it.data
                is_auth.value = !it.data?.is_auth.equals("0")

            }
        }
    }

    fun getMyCollection(type:String){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getMyCollection() }
            if(it.ok()){
                Log.e("shy", "11111" )
                list.value=it.data
            }

        }
    }



}
