package com.jld.vod.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.jld.vod.model.bean.BaseBean
import com.jld.vod.model.bean.GameResByNotUploaded
import com.jld.vod.model.bean.MovieResByNotUploaded
import com.jld.vod.model.bean.MusicResByNotUploaded
import com.jld.vod.utils.http.RetrofitUtils
import com.jld.vod.utils.http.RetrofitCallback
import java.io.File


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/1/10 13:40
 */
class PostFileViewModel(app: Application)  : AndroidViewModel(app) {

    val findResByNotUploadedmusicliveData: MutableLiveData<BaseBean<List<MusicResByNotUploaded>>> = MutableLiveData()
    val findResByNotUploadedmovieliveData: MutableLiveData<BaseBean<List<MovieResByNotUploaded>>> = MutableLiveData()
    val findResByNotUploadedgameliveData: MutableLiveData<BaseBean<List<GameResByNotUploaded>>> = MutableLiveData()
    val movieUploadliveData: MutableLiveData<BaseBean<String>> = MutableLiveData()
    val musicUploadliveData: MutableLiveData<BaseBean<String>> = MutableLiveData()
    val gameUploadliveData: MutableLiveData<BaseBean<String>> = MutableLiveData()
    /**
     * 获取音乐未上传列表
     */
    fun findResByNotUploadedmusic(): MutableLiveData<BaseBean<List<MusicResByNotUploaded>>> {
        findResByNotUploadedmusicliveData.postValue(BaseBean.loading(null))
        RetrofitUtils.findResByNotUploadedmusic(
            { t ->
                when (t.code) {
                    20000 ->{
                        findResByNotUploadedmusicliveData.postValue(BaseBean.success(t.data))
                    }
                    else -> findResByNotUploadedmusicliveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                findResByNotUploadedmusicliveData.postValue(BaseBean.error(ex.message!!, null))
            })
        return findResByNotUploadedmusicliveData
    }

    /**
     * 获取电影未上传列表
     */
    fun findResByNotUploadedmovie(): MutableLiveData<BaseBean<List<MovieResByNotUploaded>>> {
        findResByNotUploadedmovieliveData.postValue(BaseBean.loading(null))
        RetrofitUtils.findResByNotUploadedmovie(
            { t ->
                when (t.code) {
                    20000 ->{
                        findResByNotUploadedmovieliveData.postValue(BaseBean.success(t.data))
                    }
                    else -> findResByNotUploadedmovieliveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                findResByNotUploadedmovieliveData.postValue(BaseBean.error(ex.message!!, null))
            })
        return findResByNotUploadedmovieliveData
    }

    /**
     * 获取游戏未上传列表
     */
    fun findResByNotUploadedgame(): MutableLiveData<BaseBean<List<GameResByNotUploaded>>> {
        findResByNotUploadedgameliveData.postValue(BaseBean.loading(null))
        RetrofitUtils.findResByNotUploadedgame(
            { t ->
                when (t.code) {
                    20000 ->{
                        findResByNotUploadedgameliveData.postValue(BaseBean.success(t.data))
                    }
                    else -> findResByNotUploadedgameliveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                findResByNotUploadedmovieliveData.postValue(BaseBean.error(ex.message!!, null))
            })
        return findResByNotUploadedgameliveData
    }

    /**
     * 文件电影上传（POST请求）
     */
    fun movieUpload(file: File,mid: Long,resId: Long, callback: RetrofitCallback<*>): MutableLiveData<BaseBean<String>> {
        movieUploadliveData.postValue(BaseBean.loading(null))

        RetrofitUtils.movieUpload(file,mid,resId,callback,
            { t ->
                when (t.code) {
                    20000 ->{
                        movieUploadliveData.postValue(BaseBean.success(t.data))
                    }
                    else -> movieUploadliveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                movieUploadliveData.postValue(BaseBean.error(ex.message!!, null))
            }
        )
        return movieUploadliveData
    }

    /**
     * 文件音乐上传（POST请求）
     */
    fun musicUpload(file: File,mid: Long,resId: Long, callback: RetrofitCallback<*>): MutableLiveData<BaseBean<String>> {
        musicUploadliveData.postValue(BaseBean.loading(null))

        RetrofitUtils.musicUpload(file,mid,resId,callback,
            { t ->
                when (t.code) {
                    20000 ->{
                        musicUploadliveData.postValue(BaseBean.success(t.data))
                    }
                    else -> musicUploadliveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                musicUploadliveData.postValue(BaseBean.error(ex.message!!, null))
            }
        )
        return musicUploadliveData
    }

    /**
     * 文件游戏上传（POST请求）
     */
    fun gameUpload(file: File,mid: Long,resId: Long, callback: RetrofitCallback<*>): MutableLiveData<BaseBean<String>> {
        gameUploadliveData.postValue(BaseBean.loading(null))

        RetrofitUtils.gameUpload(file,mid,resId,callback,
            { t ->
                when (t.code) {
                    20000 ->{
                        gameUploadliveData.postValue(BaseBean.success(t.data))
                    }
                    else -> gameUploadliveData.postValue(BaseBean.error(t.message, t.data))
                }
            },
            { ex ->
                gameUploadliveData.postValue(BaseBean.error(ex.message!!, null))
            }
        )
        return gameUploadliveData
    }
}

