package com.adealink.weparty.applyskill

import androidx.fragment.app.FragmentActivity
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.log.Log
import com.adealink.frame.router.Router
import com.adealink.frame.router.interceptor.UriInterceptor
import com.adealink.frame.router.request.UriRequest
import com.adealink.weparty.App
import com.adealink.weparty.applyskill.data.SkillListReq
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.isFailure
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.joinus.data.JoinUsData
import com.adealink.weparty.joinus.data.TAG_JOINUS
import com.adealink.weparty.joinus.datasource.remote.JoinUsHttpService
import com.adealink.weparty.module.category.Category
import com.adealink.weparty.module.category.data.CategoryAction
import com.adealink.weparty.module.joinus.JoinUs
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.util.parcelableExtra
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import com.adealink.weparty.R as APP_R

class ApplySkillInterceptor : UriInterceptor {

    private val joinUsHttpService by lazy {
        App.instance.networkService.getHttpService(JoinUsHttpService::class.java)
    }

    override fun intercept(chain: UriInterceptor.Chain) {
        val request = chain.request()
        val activity = request.ctx as? FragmentActivity
        if (activity == null) {
            Log.e(TAG_JOINUS, "JoinUsInterceptor, activity is null")
            return
        }
        CoroutineScope(Dispatcher.UI).launch {
            (activity as? BaseActivity)?.showLoading()

            //1. 检查当前是否有技能处于申请状态
            val applyRlt = loadCurrentApplyInfo()
            if (applyRlt.isFailure) {
                (activity as? BaseActivity)?.dismissLoading()
                showToast(applyRlt)
                chain.abort()
                return@launch
            }

            val applyInfo = (applyRlt as Rlt.Success).data
            //当前已经有申请处于审核状态
            if (applyInfo?.underReview == true) {
                (activity as? BaseActivity)?.dismissLoading()
                request.intent.putExtra(JoinUs.Common.EXTRA_JOIN_US, applyInfo)
                chain.proceed(request)
                return@launch
            }

            //没有处于申请状态
            val applyCategory = request.intent.parcelableExtra(
                JoinUs.ApplySKill.EXTRA_CATEGORY,
                PlaymateCategoryData::class.java
            )
            if (applyCategory != null) {
                (activity as? BaseActivity)?.dismissLoading()
                chain.proceed(request)
                return@launch
            }

            //跳转品类选择页面
            goSelectCategoryPage(activity, request, chain)
            chain.abort()
        }
    }

    private suspend fun goSelectCategoryPage(
        activity: FragmentActivity,
        request: UriRequest,
        chain: UriInterceptor.Chain
    ) {
        var ownList = request.intent.getStringArrayListExtra(JoinUs.ApplySKill.EXTRA_OWN_SKILL_LIST)
        if (ownList.isNullOrEmpty()) {
            val rlt = loadOwnSkillList()
            if (rlt.isFailure) {
                (activity as? BaseActivity)?.dismissLoading()
                showToast(rlt)
                chain.abort()
                return
            }

            ownList = arrayListOf<String>().apply {
                addAll((rlt as Rlt.Success).data)
            }
        }

        (activity as? BaseActivity)?.dismissLoading()
        Router.build(activity, Category.CategoryList.PATH)
            .putExtra(
                Category.CategoryList.EXTRA_TITLE,
                getCompatString(APP_R.string.common_select_category)
            )
            .putExtra(
                Category.CategoryList.EXTRA_SELECT_ACTION,
                CategoryAction.APPLY_SKILL.action
            )
            .putStringArrayListExtra(
                Category.CategoryList.EXTRA_HIDDEN_CATEGORY,
                arrayListOf<String>().apply {
                    addAll(ownList)
                }
            )
            .start()

    }

    private suspend fun loadCurrentApplyInfo(): Rlt<JoinUsData?> {
        val rlt = joinUsHttpService.loadJoinUsProgress()
        return when (rlt) {
            is Rlt.Failed -> {
                rlt
            }

            is Rlt.Success -> {
                Rlt.Success(rlt.data.data)
            }
        }
    }

    private suspend fun loadOwnSkillList(): Rlt<List<String>> {
        val rlt = joinUsHttpService.getSkillSettingList(SkillListReq())
        return when (rlt) {
            is Rlt.Failed -> {
                rlt
            }

            is Rlt.Success -> {
                val list = rlt.data.data?.list?.map { it.categoryCode } ?: emptyList()
                Rlt.Success(list)
            }
        }
    }

}