package com.wdkl.ncs.android.middleware.api

import android.annotation.SuppressLint
import com.wdkl.ncs.android.lib.base.BaseApplication
import com.wdkl.ncs.android.lib.utils.UUID
import okhttp3.*

/**
 * @author  LDD
 * @Date   2018/1/11 下午6:12
 * @From   com.wdkl.ncs.android.lib.api
 * @Note   API管理器
 */
object ApiManager {

        /**
         * @Name  apiFactory
         * @Type  NetFectory
         * @Note  API构建者
         */
        @SuppressLint("StaticFieldLeak")
        private val apiFactory = NetFactory.build(BaseApplication.appContext)

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.BASE_URL
         * @Type  UrlManager
         * @Note  服务器URL控制器
         */
        private val urlManager = UrlManager.build()

        /** 全局分页条数 */
        val PAGE_SIZE = 10

        /**
         * @Name  com.wdkl.ncs.android.middleware.api.ApiManager.EXTRA_API
         * @Type  ExtraApi
         * @Note  其他API
         */
        val EXTRA_API :ExtraApi by lazy { apiFactory.createService(ExtraApi::class.java, urlManager.buyer) }


        /**
         * @Name  com.wdkl.ncs.android.middleware.api.ApiManager.BASE_URL
         * @Type  BaseApi
         * @Note  基础API
         */
        val BASE_API :BaseApi by lazy { apiFactory.createService(BaseApi::class.java, urlManager.basic)}

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_AFTERSALE
         * @Type  AfterSaleApi
         * @Note  售后API
         */
        val API_AFTERSALE : AfterSaleApi by lazy { apiFactory.createService(AfterSaleApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_CART
         * @Type  CartApi
         * @Note  购物车API
         */
        val API_CART: CartApi by lazy { apiFactory.createService(CartApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_CATEGORY
         * @Type  CategoryApi
         * @Note  分类API
         */
        val API_CATEGORY: CategoryApi by lazy { apiFactory.createService(CategoryApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_GOODS
         * @Type  GoodsApi
         * @Note  商品API
         */
        val API_GOODS: GoodsApi by lazy { apiFactory.createService(GoodsApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_MEMBER
         * @Type  MemberApi
         * @Note  会员API
         */
        val API_MEMBER: MemberApi by lazy { apiFactory.createService(MemberApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_ORDER
         * @Type  OrderApi
         * @Note  订单API
         */
        val API_ORDER: OrderApi by lazy { apiFactory.createService(OrderApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_PAYMENT
         * @Type  PaymentApi
         * @Note  支付API
         */
        val API_PAYMENT: PaymentApi by lazy { apiFactory.createService(PaymentApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_PROMOTION
         * @Type  PromotionApi
         * @Note  促销API
         */
        val API_PROMOTION: PromotionApi by lazy { apiFactory.createService(PromotionApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_SHOP
         * @Type  ShopApi
         * @Note  店铺API
         */
        val API_SHOP: ShopApi by lazy { apiFactory.createService(ShopApi::class.java, urlManager.buyer) }

        /**
         * @Name  com.wdkl.ncs.android.lib.api.ApiManager.Companion.API_TRADE
         * @Type  TradeApi
         * @Note  交易API
         */
        val API_TRADE: TradeApi by lazy { apiFactory.createService(TradeApi::class.java, urlManager.buyer) }

        /**
         * @Name  API_AD
         * @Type  AdApi
         * @Note  广告API
         */
        val API_AD: AdApi by lazy { apiFactory.createService(AdApi::class.java, urlManager.buyer) }

        /**
         * @Name  PASSPORT_API
         * @Type  PassportApi
         * @Note  护照
         */
        val PASSPORT_API :PassportApi by lazy { apiFactory.createService(PassportApi::class.java, urlManager.buyer) }

        val API_HELLO :HelloApi by lazy { apiFactory.createService(HelloApi::class.java, urlManager.buyer)}

        //5寸分机
        val API_CALLINGBED :CallingbedApi by lazy { apiFactory.createService(CallingbedApi::class.java, urlManager.buyer) }

        /**
         * @author LDD
         * @From   ApiManager
         * @Date   2018/8/13 上午8:19
         * @Note   刷新Token
         * @param  refrehToken 刷新Token
         */
        fun refreshToken(refrehToken :String) : Response{
                val mediaType = MediaType.parse("text/x-markdown; charset=utf-8")
                val requestBody = "refrehToken"
                val okHttpClient = OkHttpClient()
                val getTokenRequest = Request.Builder()
                        .url("${urlManager.buyer}passport/token?refersh_token=$refrehToken")
                        .post(RequestBody.create(mediaType,requestBody))
                        .addHeader("uuid", UUID.uuid)
                        .build()
                val call = okHttpClient.newCall(getTokenRequest)
                return call.execute()
        }
}
