package com.jld.vod.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.room.Room
import com.jld.vod.db.CarInfoDatabase
import com.jld.vod.db.LanguageDatabase
import com.jld.vod.db.ModularAllDatabase
import com.jld.vod.model.bean.*
import com.jld.vod.utils.http.RetrofitUtils

/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/7/22 17:15
 */
class MainViewModel (app: Application)  : AndroidViewModel(app)  {

    val modularalldb = Room.databaseBuilder(app, ModularAllDatabase::class.java, "modularall").build()
    val carinfodb = Room.databaseBuilder(app, CarInfoDatabase::class.java, "carinfo").build()
    val languagedb = Room.databaseBuilder(app, LanguageDatabase::class.java, "language").build()

    val modelAllLiveData: MutableLiveData<BaseBean<List<ModelAllBean>>> = MutableLiveData()
    val findAllModularImgLiveData: MutableLiveData<BaseBean<findAllModularImgBean>> = MutableLiveData()
    val findAllModularRotateImgBeanLiveData: MutableLiveData<BaseBean<List<AllModularRotateImgBean>>> = MutableLiveData()
    val findPositionAdvertLiveData: MutableLiveData<BaseBean<List<PositionAdvertBean>>> = MutableLiveData()

    val carinfoLiveData: MutableLiveData<List<CarInfoBean>> = MutableLiveData()
    val ByrtNamesLiveData: MutableLiveData<List<ModelAllBean>> = MutableLiveData()
    val LanguageLiveData: MutableLiveData<List<LanguageBean>> = MutableLiveData()

    /**
     * 获取全部模块信息
     */
    fun findModularAll(userId:Long): MutableLiveData<BaseBean<List<ModelAllBean>>> {
        modelAllLiveData.postValue(BaseBean.loading(null))
        RetrofitUtils.findModularAll(userId,
            { t ->
               // LogUtils.logD("findModularAll"+t)
                when (t.code) {
                    20000 ->{
                        if (t.data!=null)
                        {
                            Thread(Runnable{ run {
                                if (!modularalldb.modularAllDao().getAll().isEmpty())
                                {
                                    modularalldb.modularAllDao().delete()
                                    for ((index,e) in t.data.withIndex()){
                                        modularalldb.modularAllDao().insertAll(t.data[index])
                                    }
                                }else{
                                    for ((index,e) in t.data.withIndex()){
                                        modularalldb.modularAllDao().insertAll(t.data[index])
                                    }
                                }
                            }}).start()
                        }
                        modelAllLiveData.postValue(BaseBean.success(t.data))
                    }
                    else -> modelAllLiveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                ex.printStackTrace()
                modelAllLiveData.postValue(BaseBean.error(ex.message!!, null))
            }
        )
        return modelAllLiveData
    }

    /**
     * 获取所有静态资源
     */
    fun findAllModularImg(userId:String): MutableLiveData<BaseBean<findAllModularImgBean>> {
       // findAllModularImgLiveData.postValue(BaseBean.loading(null))
        RetrofitUtils.findAllModularImg(userId,
            { t ->
                when (t.code) {
                    20000 ->{
                        findAllModularImgLiveData.postValue(BaseBean.success(t.data))
                    }
                    else -> findAllModularImgLiveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                ex.printStackTrace()
                findAllModularImgLiveData.postValue(BaseBean.error(ex.message!!, null))
            }
        )
        return findAllModularImgLiveData
    }

    /**
     * 获取所有静态资源
     */
    fun findAllModularRotateImg(userId:String,index: Int): MutableLiveData<BaseBean<List<AllModularRotateImgBean>>> {
         findAllModularImgLiveData.postValue(BaseBean.loading(null))
            RetrofitUtils.findAllModularRotateImg(userId,index,
            { t ->

                when (t.code) {
                    20000 ->{
                        findAllModularRotateImgBeanLiveData.postValue(BaseBean.success(t.data))
                    }
                    else -> findAllModularRotateImgBeanLiveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                ex.printStackTrace()
                findAllModularRotateImgBeanLiveData.postValue(BaseBean.error(ex.message!!, null))
            }
        )
        return findAllModularRotateImgBeanLiveData
    }
    /**
     * 获取固定位置广告
     */
    fun findPositionAdvert(): MutableLiveData<BaseBean<List<PositionAdvertBean>>> {
        findPositionAdvertLiveData.postValue(BaseBean.loading(null))
        RetrofitUtils.findPositionAdvert(
                { t ->
                    when (t.code) {
                        20000 ->{
                            findPositionAdvertLiveData.postValue(BaseBean.success(t.data))
                        }
                        else -> findPositionAdvertLiveData.postValue(BaseBean.error(t.message, t.data))
                    }
                },
                { ex ->
                    ex.printStackTrace()
                    findAllModularRotateImgBeanLiveData.postValue(BaseBean.error(ex.message!!, null))
                }
        )
        return findPositionAdvertLiveData
    }
    /**
     * 获取车辆信息
     */
    fun getCarInfo() {
        Thread(Runnable {
            carinfoLiveData!!.postValue(carinfodb.carInfoDao().getAll())
        }).start()
    }

    /**
     * 获取单个模块
     */
    fun getByrtNamesModular(rtName:String) {
        Thread(Runnable {
            ByrtNamesLiveData!!.postValue(modularalldb.modularAllDao().loadAllByrtNames(rtName))
        }).start()
    }

    /**
     * 获取全部模块
     */
    fun getAllModular() {
        Thread(Runnable {
            ByrtNamesLiveData!!.postValue(modularalldb.modularAllDao().getAll())
        }).start()
    }

    /**
     * 获取所有语言
     */
    fun getLanguage() {
        Thread(Runnable {
            LanguageLiveData!!.postValue(languagedb.languageDao().getAll())
        }).start()
    }
}
