package com.duben.microtribe.net;

import android.content.Context;
import android.text.TextUtils;

import com.duben.microtribe.mvp.model.RecommendBannerList;
import com.google.gson.JsonObject;
import com.duben.microtribe.BuildConfig;
import com.duben.microtribe.mvp.model.BannerList;
import com.duben.microtribe.mvp.model.BaseResponse;
import com.duben.microtribe.mvp.model.BannerList;
import com.duben.microtribe.mvp.model.HotStyleTypesList;
import com.duben.microtribe.mvp.model.IndexList;
import com.duben.microtribe.mvp.model.NineListBean;
import com.duben.microtribe.mvp.model.NineShowBean;
import com.duben.microtribe.mvp.model.OrderRecordBean;
import com.duben.microtribe.mvp.model.RecoBean;
import com.duben.microtribe.mvp.model.UserBean;
import com.duben.microtribe.mvp.model.VedioBean;
import com.duben.microtribe.mvp.model.Version;
import com.duben.microtribe.mvp.model.VipBean;
import com.duben.microtribe.mvp.model.WxPayParamBean;
import com.duben.microtribe.utils.AESUtils;
import com.orhanobut.logger.Logger;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.http.Body;
import retrofit2.http.POST;
import rx.Observable;

/**
 * 描述：服务器交互
 * 作者：孟崔广
 */
public interface LoanService {

    /**
     * 更新版本
     *
     * @return
     */
    @POST("appNa/checkUpgrade")
    Observable<BaseResponse<Version>> checkUpgrade(@Body Map<String, Object> vo);

    /**
     * 游客登录
     *
     * @return
     */
    @POST("api/user/visitorlogin")
    Observable<BaseResponse<UserBean>> visitorlogin(@Body Map<String, Object> vo);

    /**
     * 获取验证码
     *
     * @return
     */
    @POST("api/user/sendMobileCode")
    Observable<BaseResponse<Object>> sendMobileCode(@Body Map<String, Object> vo);

    /**
     * 登录
     *
     * @return
     */
    @POST("api/user/mobilelogin")
    Observable<BaseResponse<UserBean>> login(@Body Map<String, Object> vo);

    /**
     * 微信登录
     *
     * @return
     */
    @POST("api/user/wechatlogin")
    Observable<BaseResponse<UserBean>> wechatlogin(@Body Map<String, Object> vo);

    /**
     * 提交设备信息
     *
     * @return
     */
    @POST("api/user/saveTerminalInfo")
    Observable<BaseResponse<Object>> saveTerminalInfo(@Body Map<String, Object> vo);

    /**
     * 查询vip产品
     */
    @POST("api/vip/getVipProducts/appv2")
    Observable<BaseResponse<VipBean>> getVipProducts(@Body Map<String, Object> vo);

    /**
     * 查询失败
     *
     * @return
     */
    @POST("api/vip/payError")
    Observable<BaseResponse<Object>> payError(@Body Map<String, Object> vo);

    /**
     * 获取支付参数
     *
     * @return
     */
    @POST("api/vip/getVipPayParams")
    Observable<BaseResponse<WxPayParamBean>> getVipPayParams(@Body Map<String, Object> vo);

    /**
     * 查询vip订单
     *
     * @return
     */
    @POST("api/vip/queryVipOrder")
    Observable<BaseResponse<Object>> queryVipOrder(@Body Map<String, Object> vo);

    /**
     * 订单记录
     *
     * @return
     */
    @POST("api/vip/getOrderList")
    Observable<BaseResponse<OrderRecordBean>> getCashOutRecords();

    /**
     * 我的
     *
     * @return
     */
    @POST("api/user/baseMsg")
    Observable<BaseResponse<UserBean>> getMyInfo(@Body Map<String, Object> vo);

    /**
     * 申请退款
     *
     * @return
     */
    @POST("api/vip/reqfundV1")
    Observable<BaseResponse<JsonObject>> reqfundV1(@Body Map<String, Object> vo);

    /**
     * 取消自动续费
     *
     * @return
     */
    @POST("api/vip/unSign")
    Observable<BaseResponse<JsonObject>> unSign();

    /**
     * groMore激励视频展示广告提交数据
     *
     * @return
     */
    @POST("api/vedioV1/unlock")
    Observable<BaseResponse<IndexList>> unlock(@Body Map<String, Object> vo);

    /**
     * 首页分类
     *
     * @return
     */
    @POST("api/vedioV1/types")
    Observable<BaseResponse<HotStyleTypesList>> getHomeV1Types();

    /**
     * 首页列表
     *
     * @return
     */
    @POST("api/vedioV1/listByType")
    Observable<BaseResponse<BannerList>> getHomeV1List(@Body Map<String, Object> vo);

    /**
     * 用户反馈
     *
     * @return
     */
    @POST("api/vedioV1/feedback")
    Observable<BaseResponse<Object>> feedback(@Body Map<String, Object> vo);

    /**
     * 轮播图
     *
     * @return
     */
    @POST("api/vedioV1/topTabs/v0821")
    Observable<BaseResponse<BannerList>> topTabs();

    /**
     * 排行榜
     *
     * @return
     */
    @POST("api/vedioV1/orders")
    Observable<BaseResponse<BannerList>> orders();


    /**
     * 剧情列表
     *
     * @return
     */
    @POST("api/vedioV1/getIndexList")
    Observable<BaseResponse<IndexList>> getIndexList(@Body Map<String, Object> vo);

    /**
     * 优选剧 剧情列表
     *
     * @return
     */
    @POST("api/vedioV1/getIndexList")
    Observable<BaseResponse<IndexList>> getIndexList2(@Body Map<String, Object> vo);

    /**
     * 播放链接
     *
     * @return
     */
    @POST("api/vedioV1/getIndex")
    Observable<BaseResponse<Object>> getIndex(@Body Map<String, Object> vo);

    /**
     * 收藏
     *
     * @return
     */
    @POST("api/vedioV1/collect")
    Observable<BaseResponse<Object>> collect(@Body Map<String, Object> vo);

    /**
     * 取消收藏
     *
     * @return
     */
    @POST("api/vedioV1/cancelCollect")
    Observable<BaseResponse<Object>> cancelCollect(@Body Map<String, Object> vo);

    /**
     * 收藏记录
     *
     * @return
     */
    @POST("api/vedioV1/collectList")
    Observable<BaseResponse<BannerList>> collectList();

    /**
     * 最新一条记录
     *
     * @return
     */
    @POST("api/url/newestRecord")
    Observable<BaseResponse<Object>> newestRecord();

    /**
     * 历史记录
     *
     * @return
     */
    @POST("api/vedioV1/history")
    Observable<BaseResponse<BannerList>> history();

    /**
     * 提交视频信息
     */
    @POST("api/vedioV1/reportIndex")
    Observable<BaseResponse<Object>> reportIndex(@Body Map<String, Object> vo);

    /**
     * 推荐
     */
    @POST("api/vedioV1/autoList")
    Observable<BaseResponse<BannerList>> autoList();

    /**
     * 穿山甲上传剧集
     */
    @POST("api/vedioV1/initCsj")
    Observable<BaseResponse<Object>> commitCsjShotInfo(@Body Map<String, Object> vo);

    /**
     * 推荐
     */
    @POST("api/vedioV1/firstVedio")
    Observable<BaseResponse<IndexList>> getSoltVedio();

    /**
     * 匹配推荐
     */
    @POST("api/vedioV1/vedio3Msg")
    Observable<BaseResponse<RecoBean>> getRecommendVedio();

    /**
     * 9宫格信息
     */
    @POST("api/vedioV1/getTurnConfig")
    Observable<BaseResponse<NineListBean>> getTurnConfig();

    /**
     * 9宫格抽奖
     */
    @POST("api/vedioV1/toTurn")
    Observable<BaseResponse<NineListBean.NineBean>> toTurn();

    /**
     * 是否展示9宫格
     */
    @POST("api/vedioV1/showTurn")
    Observable<BaseResponse<NineShowBean>> showTurn();

    /**
     * 获取推荐状态
     */
    @POST("api/vedioV1/showVedio4")
    Observable<BaseResponse<JsonObject>> firstShowVedio();

    /**
     * 推荐banner
     */
    @POST("api/vedioV1/vedio4Msg")
    Observable<BaseResponse<RecommendBannerList>> vedio4Msg(@Body Map<String, Object> vo);

    /**
     * Vip尊享剧场
     */
    @POST("api/vedioV1/vedio4List")
    Observable<BaseResponse<BannerList>> vedio4List(@Body Map<String, Object> vo);

    /**
     * 默认http工厂
     */
    class Factory {
        public static LoanService create(Context context) {

            OkHttpClient.Builder builder = new OkHttpClient.Builder();

            builder.retryOnConnectionFailure(true);
            builder.connectTimeout(20, TimeUnit.SECONDS);
            builder.readTimeout(20, TimeUnit.SECONDS);
            builder.writeTimeout(20, TimeUnit.SECONDS);

//            if (BuildConfig.DEBUG) {
//                HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//                logging.setLevel(HttpLoggingInterceptor.Level.BODY);
//                builder.interceptors().add(logging);
//            }

            if (BuildConfig.DEBUG) {
                HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
                    if (TextUtils.isEmpty(message)) return;
                    String s = message.substring(0, 1);
                    String request = message.substring(0, 4);
                    if ("{".equals(s) || "[".equals(s)) {
                        Logger.json(message);
                    } else if (request.contains("-->") || request.contains("<--")) {
                        Logger.d("Method" + message);
                    } else {
                        Logger.d("params:" + message);
                    }
                });
                logging.setLevel(HttpLoggingInterceptor.Level.BODY);
                builder.interceptors().add(logging);
            }

            OkHttpInterceptor okHttpInterceptor = new OkHttpInterceptor(AESUtils.getDefaultKey());
            builder.interceptors().add(okHttpInterceptor);

            Retrofit retrofit = new Retrofit.Builder()
                    .client(builder.build())
                    .baseUrl(BuildConfig.MainIp)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();


            return retrofit.create(LoanService.class);
        }
    }
}
