package com.develop.food.base.api

import androidx.annotation.UiThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.develop.food.base.api.request.*
import com.develop.food.base.api.response.*
import com.develop.food.base.http.Resource
import com.develop.food.base.http.checkDataAndSignal


object FoodRepository {

    private val registerLiveData by lazy {
        MutableLiveData<Resource<RegisterResult>>()
    }

    private val loginLiveData by lazy {
        MutableLiveData<Resource<LoginResult>>()
    }

    private val perfectInfoLiveData by lazy {
        MutableLiveData<Resource<PerfectInfoResult>>()
    }

    private val scoreLiveData by lazy {
        MutableLiveData<Resource<ScoreResult>>()
    }

    private val recipesListLiveData by lazy {
        MutableLiveData<Resource<RecipesListResult>>()
    }

    private val recipeDetailLiveData by lazy {
        MutableLiveData<Resource<RecipeDetailResult>>()
    }

    private val activateLiveData by lazy {
        MutableLiveData<Resource<Any>>()
    }


    private val devInfoLiveData by lazy {
        MutableLiveData<Resource<DevInfoResult>>()
    }

    private val sendVerifyCodeLiveData by lazy {
        MutableLiveData<Resource<VerifyCodeResult>>()
    }

    private val updatePasswordLiveData by lazy {
        MutableLiveData<Resource<Any>>()
    }

    private val recipesCategoryLiveData by lazy {
        MutableLiveData<Resource<CategoryListResult>>()
    }

    private val hotTagsLiveData by lazy {
        MutableLiveData<Resource<HotTagsResult>>()
    }

    private val noticesLiveData by lazy {
        MutableLiveData<Resource<NoticeResult>>()
    }


    @UiThread
    fun register(
        account: String,
        password: String,
        forceUpdate: Boolean = true
    ): LiveData<Resource<RegisterResult>> {
        checkDataAndSignal(
            registerLiveData,
            FoodApiService.get()
                .register(RegisterBody.genRegisterBody(account, password)),
            forceUpdate
        )
        return registerLiveData
    }

    @UiThread
    fun login(
        account: String,
        password: String,
        forceUpdate: Boolean = true
    ): LiveData<Resource<LoginResult>> {
        checkDataAndSignal(
            loginLiveData,
            FoodApiService.get()
                .login(LoginBody.genLoginBody(account, password)),
            forceUpdate
        )
        return loginLiveData
    }


    @UiThread
    fun perfectInfo(
        nickname: String,
        gender: String,
        portrait: String,
        age: String,
        forceUpdate: Boolean = true
    ): LiveData<Resource<PerfectInfoResult>> {
        checkDataAndSignal(
            perfectInfoLiveData,
            FoodApiService.get()
                .perfectInfo(PerfectInfoBody.genPerfectInfoBody(nickname, gender, portrait, age)),
            forceUpdate
        )
        return perfectInfoLiveData
    }

    @UiThread
    fun score(
        recipeNumber: String,
        recipeEdition: String,
        score: String,
        forceUpdate: Boolean = true
    ): LiveData<Resource<ScoreResult>> {
        checkDataAndSignal(
            scoreLiveData,
            FoodApiService.get()
                .score(ScoreBody.genScoreBody(recipeNumber, recipeEdition, score)),
            forceUpdate
        )
        return scoreLiveData
    }


    @UiThread
    fun recipesList(
        category: String,
        name: String,
        pageIndex: Int = 1,
        pageSize: Int = 10,
        forceUpdate: Boolean = true
    ): LiveData<Resource<RecipesListResult>> {
        checkDataAndSignal(
            recipesListLiveData,
            FoodApiService.get()
                .recipesList(category, name),
            forceUpdate
        )
        return recipesListLiveData
    }

    @UiThread
    fun recipesDetail(
        recipeNumber: String,
        recipeEdition: String,
        forceUpdate: Boolean = true
    ): LiveData<Resource<RecipeDetailResult>> {
        checkDataAndSignal(
            recipeDetailLiveData,
            FoodApiService.get()
                .recipeDetail(recipeNumber, recipeEdition),
            forceUpdate
        )
        return recipeDetailLiveData
    }


    @UiThread
    fun activate(
        forceUpdate: Boolean = true
    ): LiveData<Resource<Any>> {
        checkDataAndSignal(
            activateLiveData,
            FoodApiService.get()
                .active(ActivateBody.genActivateBody()),
            forceUpdate
        )
        return activateLiveData
    }

    @UiThread
    fun devInfo(
        forceUpdate: Boolean = true
    ): LiveData<Resource<DevInfoResult>> {



        checkDataAndSignal(
            devInfoLiveData,
            FoodApiService.get()
                .deviceInfo(DeviceInfoBody.genDeviceInfoBody()),
            forceUpdate
        )
        return devInfoLiveData
    }

    @UiThread
    fun sendVerifyCode(
        account: String,
        forceUpdate: Boolean = true
    ): LiveData<Resource<VerifyCodeResult>> {
        checkDataAndSignal(
            sendVerifyCodeLiveData,
            FoodApiService.get()
                .sendVerifyCode(SendVerifyCodeBody.genSendVerifyCodeBody(account)),
            forceUpdate
        )
        return sendVerifyCodeLiveData
    }


    @UiThread
    fun updatePassword(
        account: String,
        password: String,
        verifyCodeKey: String,
        verifyCode: String,
        forceUpdate: Boolean = true
    ): LiveData<Resource<Any>> {
        checkDataAndSignal(
            updatePasswordLiveData,
            FoodApiService.get()
                .updatePassword(
                    UpdatePasswordBody.genUpdatePasswordBody(
                        account,
                        password,
                        verifyCodeKey,
                        verifyCode
                    )
                ),
            forceUpdate
        )
        return updatePasswordLiveData
    }


    @UiThread
    fun getRecipesCategory(
        forceUpdate: Boolean = true
    ): LiveData<Resource<CategoryListResult>> {
        checkDataAndSignal(
            recipesCategoryLiveData,
            FoodApiService.get()
                .getCategoryList(),
            forceUpdate
        )
        return recipesCategoryLiveData
    }

    @UiThread
    fun getHotTags(
        forceUpdate: Boolean = true
    ): LiveData<Resource<HotTagsResult>> {
        checkDataAndSignal(
            hotTagsLiveData,
            FoodApiService.get()
                .hotTags(),
            forceUpdate
        )
        return hotTagsLiveData
    }


    @UiThread
    fun getNotices(
        forceUpdate: Boolean = true
    ): LiveData<Resource<NoticeResult>> {
        checkDataAndSignal(
            noticesLiveData,
            FoodApiService.get()
                .getNotices(),
            forceUpdate
        )
        return noticesLiveData
    }

}