package com.yidianling.home.http

import com.ydl.ydlcommon.base.config.HttpConfig
import com.ydl.ydlcommon.data.http.BaseAPIResponse
import com.ydl.ydlcommon.data.http.BaseResponse
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.router.YdlCommonRouterManager
import com.ydl.ydlnet.YDLHttpUtils
import com.yidianling.home.model.bean.*
import io.reactivex.Observable

/**
 * @author jiucheng
 * @描述:首页接口实现类
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/8/22
 */
class HomeHttp private constructor() {

    companion object {
        fun getInstance(): HomeHttp {
            return Holder.INSTANCE
        }

        fun clearHomPagerApi() {
            Holder.INSTANCE.homPagerApi = null
            Holder.INSTANCE.phpHomPagerApi = null
        }
    }

    object Holder {
        val INSTANCE = HomeHttp()
    }

    private var homPagerApi: HomePagerApi? = null

    private var phpHomPagerApi: HomePagerApi? = null

    private fun getPhpHomPagerApi(): HomePagerApi {
        if (phpHomPagerApi == null) {
            phpHomPagerApi = YDLHttpUtils.obtainApi(HomePagerApi::class.java)
        }
        return phpHomPagerApi!!
    }

    private fun getHomPagerApi(): HomePagerApi {
        if (homPagerApi == null) {
            homPagerApi = YDLHttpUtils.obtainApi(HomePagerApi::class.java)
        }
        return homPagerApi!!
    }


    //首页头部 分类&Banner 请求
    fun newHomeHeaderRequest(): Observable<BaseAPIResponse<HomeHeaderBean>> {
        return getHomPagerApi().getHomeHeaderData()
    }

    //首页倾述请求
    fun getConfideData(goodType: String): Observable<BaseAPIResponse<HomeConfideBean>> {
        val loginBean = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()
        var userId = loginBean?.userId ?: "";
        return getHomPagerApi().getConfideData(goodType, uid = userId);
    }

    //首页课程请求
    fun getCourseData(listType: String = "10"): Observable<BaseAPIResponse<HomeCourseBean>> {
        val loginBean = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()
        var userId = loginBean?.userId ?: "";
        return getHomPagerApi().getCourseData(listType, uid = userId);
    }

    //首页测试请求
    fun getTestData(): Observable<BaseAPIResponse<MutableList<HomeTestItemBean>>> {
        val loginBean = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()
        var userId = loginBean?.userId ?: "";
        return getHomPagerApi().getTestData(uid = userId)
    }

    //首页问答请求
    fun getAskData(): Observable<BaseAPIResponse<HomeAskBean>> {
        val loginBean = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()
        var userId = loginBean?.userId ?: "";
        var token = loginBean?.token ?: "";
        return getHomPagerApi().getAskData(accessToken = token, uid = userId);
    }

    //首页电台请求
    fun getFMData(): Observable<BaseAPIResponse<HomeFMBean>> {
        return getHomPagerApi().getFMData();
    }

    //首页文章请求
    fun getArticleData(perPageRows: String = "4"): Observable<BaseAPIResponse<HomeArticleBean>> {
        return getHomPagerApi().getArticleData()
    }

    //首页咨询请求
    fun getConsultData(goodType: String): Observable<BaseAPIResponse<HomeConsultBean>> {
        val loginBean = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()
        var userId = loginBean?.userId ?: ""
        return getHomPagerApi().getConsultData(category = goodType, uid = userId)
    }

    //首页问答 - 点赞
    fun askZan(id: String): Observable<BaseResponse<AskResultBean>> {
        val cmd = AskRequestParamBean.ZanAction("2", id)
        return RxUtils.mapObservable(cmd).flatMap { getPhpHomPagerApi().askZan(it) }
//        return getPhpHomPagerApi().askZan(formatParams(cmd))
    }

    //首页问答 - 关注
    fun askFocus(id: String): Observable<BaseResponse<AskResultBean>> {
        val cmd = AskRequestParamBean.FocusCmd(id, "1")
        return RxUtils.mapObservable(cmd).flatMap { getPhpHomPagerApi().askFocus(it) }
    }

    //壹点冥想
    fun newMuseRequest(): Observable<BaseAPIResponse<MuseModuleBean>> {
        return getHomPagerApi().getMuseData()
    }
}