package com.mints.goodmoney.net;

import android.text.TextUtils;

import com.google.gson.JsonObject;
import com.mints.goodmoney.BuildConfig;
import com.mints.goodmoney.mvp.model.BannerBean;
import com.mints.goodmoney.mvp.model.BaseResponse;
import com.mints.goodmoney.mvp.model.DrawcashBean;
import com.mints.goodmoney.mvp.model.DrawcashRecordBean;
import com.mints.goodmoney.mvp.model.FriendHallMsgBean;
import com.mints.goodmoney.mvp.model.FriendsTaskBean;
import com.mints.goodmoney.mvp.model.GoldRecordBean;
import com.mints.goodmoney.mvp.model.MainVedioMsgBean;
import com.mints.goodmoney.mvp.model.MyInfo;
import com.mints.goodmoney.mvp.model.SignCardBean;
import com.mints.goodmoney.mvp.model.UserBean;
import com.mints.goodmoney.mvp.model.UserTaskMsgBean;
import com.mints.goodmoney.mvp.model.Version;
import com.mints.goodmoney.mvp.model.WalkBean;
import com.mints.goodmoney.mvp.model.WaterBean;
import com.mints.goodmoney.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 retrofit2.http.Url;
import rx.Observable;

/**
 * 描述：服务器交互
 * 作者：孟崔广
 * 时间：2020/10/14 10:51
 */
public interface LoanService {

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

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

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

    /**
     * 注册极光设备
     *
     * @return
     */
    @POST("api/registerDevice")
    Observable<BaseResponse<JsonObject>> registerDevice(@Body Map<String, Object> vo);

    /**
     * 打开激活
     *
     * @return
     */
    @POST("na/saveTerminalInfo")
    Observable<BaseResponse<Object>> comSaveTerminalInfo(@Body Map<String, Object> vo);

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

//    /**
//     * 我的界面信息
//     *
//     * @return
//     */
//    @POST("api/getUserHallBaseMsg")
//    Observable<BaseResponse<MyInfo>> getUserHallBaseMsg();

    /**
     * 热门活动接口
     *
     * @return
     */
    @POST("api/myHotActivity")
    Observable<BaseResponse<BannerBean>> myHotActivity(@Body Map<String, Object> vo);

    /**
     * 签到
     *
     * @return
     */
    @POST("api/signInHomePage")
    Observable<BaseResponse<Object>> signInHomePage();

    /**
     * 签到信息
     *
     * @return
     */
    @POST("api/getSignInHomePageMsg")
    Observable<BaseResponse<SignCardBean>> getSignInHomePageMsg();

    /**
     * 设置邀请成功
     *
     * @return
     */
    @POST("api/setStatusInOneDayByCarrierType")
    Observable<BaseResponse<Object>> setStatusInOneDayByCarrierType(@Body Map<String, Object> vo);

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

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

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

    /**
     * 手机号绑定
     *
     * @return
     */
    @POST("api/bindingMobile")
    Observable<BaseResponse<UserBean>> bindingMobile(@Body Map<String, Object> vo);

    /**
     * 提交任务
     *
     * @return
     */
    @POST("api/reportAddCoinMsg")
    Observable<BaseResponse<Object>> reportAddCoinMsg(@Body Map<String, Object> vo);

    /**
     * 获取用户配置信息
     *
     * @return
     */
    @POST("api/getCoinMsg")
    Observable<BaseResponse<JsonObject>> getCoinMsg();

    /**
     * 穿山甲回调确认
     *
     * @return
     */
    @POST("api/checkCoinRecordByTid")
    Observable<BaseResponse<Object>> checkCoinRecordByTid(@Body Map<String, Object> vo);

    /**
     * 调用事件
     *
     * @return
     */
    @POST("stasticis/adCall/add")
    Observable<BaseResponse<Object>> addCallImp(@Body Map<String, Object> vo);

    /**
     * 离线收益
     *
     * @return
     */
    @POST("api/offline")
    Observable<BaseResponse<Object>> offline();

    /**
     * 猎豹游戏 上传信息
     *
     * @return
     */
    @POST
    Observable<BaseResponse<Object>> gameAdd(@Url String url, @Body Map<String, Object> vo);

    /**
     * 获取提现信息
     *
     * @return
     */
    @POST("api/getUserCoinMsg")
    Observable<BaseResponse<DrawcashBean>> getDrawcashUserTaskMsg();

    /**
     * 微信绑定
     *
     * @return
     */
    @POST("api/bindingWechat")
    Observable<BaseResponse<UserBean>> bindingWechat(@Body Map<String, Object> vo);

    /**
     * 提现
     *
     * @return
     */
    @POST("api/addCashoutReq")
    Observable<BaseResponse<Object>> addCashoutReq(@Body Map<String, Object> vo);

    /**
     * 提现记录
     *
     * @return
     */
    @POST("api/getCashOutRecords")
    Observable<BaseResponse<DrawcashRecordBean>> getCashOutRecords();

    /**
     * 金币明细
     *
     * @return
     */
    @POST("api/getCoinRecords")
    Observable<BaseResponse<GoldRecordBean>> getCoinRecords();

    /**
     * 世界人民排行信息
     *
     * @return
     */
    @POST("api/getLeaders")
    Observable<BaseResponse<FriendsTaskBean.FriendsBean>> getLeaders();

    /**
     * 好友任务信息
     *
     * @return
     */
    @POST("api/getFriendsMsg")
    Observable<BaseResponse<FriendsTaskBean>> getFriendsMsg();

    /*
     * 首页获取喝水打卡信息
     *
     * @return
     */
    @POST("api/getWaterMsg")
    Observable<BaseResponse<WaterBean>> getWaterMsg();

    /**
     * 首页喝水打卡
     *
     * @return
     */
    @POST("api/clickForWater")
    Observable<BaseResponse<JsonObject>> clickForWater();

    /**
     * 首页走路打卡
     *
     * @return
     */
    @POST("api/clickForWalk")
    Observable<BaseResponse<JsonObject>> clickForWalk();

    /**
     * 首页走路步数
     *
     * @return
     */
    @POST("api/getWalkMsg")
    Observable<BaseResponse<WalkBean>> getWalkMsg();

    /**
     * 获取用户配置信息
     *
     * @return
     */
    @POST("api/getHallBaseMsg")
    Observable<BaseResponse<UserTaskMsgBean>> getHallBaseMsg();

    /**
     * 视频信息
     *
     * @return
     */
    @POST("api/getHomeVedioBaseMsg")
    Observable<BaseResponse<MainVedioMsgBean>> getHomeVedioMsg();

    /**
     * 横版视频信息
     *
     * @return
     */
    @POST("api/getSmallHomeVedioBaseMsg")
    Observable<BaseResponse<MainVedioMsgBean>> getSmallHomeVedioBaseMsg();

    /**
     * 阅读时长
     *
     * @return
     */
    @POST("api/addReadSeconds")
    Observable<BaseResponse<JsonObject>> addReadSeconds(@Body Map<String, Object> vo);

    /**
     * 阅读信息
     *
     * @return
     */
    @POST("api/getReadMsg")
    Observable<BaseResponse<JsonObject>> getReadInfo();

    /**
     * 游戏信息
     *
     * @return
     */
    @POST("api/game/baseMsg")
    Observable<BaseResponse<JsonObject>> getGameInfo();

    /**
     * 分红奖励数据
     *
     * @return
     */
    @POST("api/friendHallMsg")
    Observable<BaseResponse<FriendHallMsgBean>> friendHallMsg();

    /**
     * 贡献榜单
     *
     * @return
     */
    @POST("api/contributionLeaders")
    Observable<BaseResponse<FriendHallMsgBean>> contributionLeaders();

    /**
     * 收益榜单
     *
     * @return
     */
    @POST("api/contributionOutToCash")
    Observable<BaseResponse<JsonObject>> contributionOutToCash();

    /**
     * 新版 我的界面信息
     *
     * @return
     */
    @POST("api/getAutoUserHallBaseMsg")
    Observable<BaseResponse<MyInfo>> getAutoUserHallBaseMsg();


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

            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);
        }
    }
}
