package com.yidianling.course.coursePlay.common.net;

import android.content.Context;

import com.yidianling.course.bean.CourseCouponBean;
import com.yidianling.course.bean.PostersShareBean;
import com.yidianling.course.courseNew.home.ResultBean;
import com.yidianling.course.coursePlay.moudle.Course;
import com.yidianling.course.coursePlay.moudle.CourseAddOrderBean;
import com.yidianling.course.coursePlay.moudle.CoursePlayBean;
import com.yidianling.course.model.CourseCategoryBean;
import com.yidianling.course.model.TopicCourseBean;
import com.yidianling.ydlcommon.http.BaseAPIResponse;
import com.yidianling.ydlcommon.http.BaseCommand;
import com.yidianling.ydlcommon.http.BaseResponse;
import com.yidianling.ydlcommon.http.FormatText;
import com.yidianling.ydlcommon.http.YdlRetrofitUtils;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Retrofit;

import static com.yidianling.ydlcommon.http.YdlRetrofitUtils.getMaps;
import static com.yidianling.ydlcommon.http.YdlRetrofitUtils.getPostList;

/**
 * Created by Jim on 2018/2/26 0026.
 */

public class RetrofitUtils {

    public static void handleError(Context context, Throwable e) {
        YdlRetrofitUtils.handleError(context, e);
    }

    public static Retrofit getRxRetrofit() {
        return YdlRetrofitUtils.getRxRetrofit();
    }


    //课程提交回复
    public static Observable<BaseResponse<Object>> commitCourseReply(Command.CourseCommitReply cmd) {
        List<FormatText> list = getPostList(cmd);
        return YdlRetrofitUtils.getRxRetrofit().newBuilder().baseUrl(YdlRetrofitUtils.SERVER_API_JAVA_URL).build().create(NetApiStore.class).commitCourseReply(getMaps(list));
    }

    //获取课程海报分享数据
    public static Observable<BaseResponse<PostersShareBean>> getPosterData(String courseId) {
        return YdlRetrofitUtils.getRxRetrofit().newBuilder().baseUrl(YdlRetrofitUtils.SERVER_API_JAVA_URL).build().create(NetApiStore.class).getPosterData(courseId);
    }


    //课程播放页--点赞
    public static Observable<BaseResponse<Object>> coursePlayZan(Command.CoursePlayZan cmd) {
        List<FormatText> list = getPostList(cmd);
        return getRxRetrofit().create(NetApiStore.class).coursePlayZan(getMaps(list));
    }

    //课程课后笔记列表
    public static Observable<BaseResponse<List<CoursePlayBean.Comments>>> courseReplyList(Command.CourseReplyList cmd) {
        List<FormatText> list = getPostList(cmd);
        return getRxRetrofit().create(NetApiStore.class).courseReplyList(getMaps(list));
    }

    public static Observable<BaseAPIResponse<List<ResultBean>>> getCoursesHomePage() {
        return getRxRetrofit().newBuilder().baseUrl(YdlRetrofitUtils.SERVER_API_JAVA_URL).build().create(NetApiStore.class).getCourseHomePageDatas();
    }

    //获取课程列表(php接口)
    public static Observable<BaseResponse<List<Course>>> getCourseList(Command.GetCourse cmd) {
        List<FormatText> list = getPostList(cmd);
        return getRxRetrofit().create(NetApiStore.class).getPhpCourseList(getMaps(list));
    }

    //获取课程列表分类
    public static Observable<BaseResponse<List<CourseCategoryBean>>> getCourseListCategory(BaseCommand cmd) {
        List<FormatText> list = getPostList(cmd);
        return getRxRetrofit().create(NetApiStore.class).getCourseCats(getMaps(list));
    }

    //获取课程列表分类
    public static Observable<BaseAPIResponse<CourseCategoryBean>> getCourseListCategory() {
        return getRxRetrofit().newBuilder().baseUrl(YdlRetrofitUtils.SERVER_API_JAVA_URL).build().create(NetApiStore.class).getCourseListCategory();
    }

    //获取课程专题
    public static Observable<BaseAPIResponse<TopicCourseBean>> getCourseTopic(String id) {
        return getRxRetrofit().newBuilder().baseUrl(YdlRetrofitUtils.SERVER_API_JAVA_URL).build().create(NetApiStore.class).getCourseSpecial(id);
    }


    /**
     * 获取可以领取的优惠券
     *
     * @param uid         uid
     * @param services    适用服务 1预约咨询 2电话倾诉 3课程（用户课程 专家课程） 5测评
     * @param receiveType 领取条件方式 1前台领取 2链接领取
     * @param applyEnds   适用终端 1PC官网 2wap站点 3用户app 4好杮app 5专家app
     * @return
     */
    public static Observable<BaseResponse<List<CourseCouponBean>>> getCourseCoupons(String uid, String services, String receiveType, String applyEnds) {
        return YdlRetrofitUtils.getRxRetrofit().newBuilder().baseUrl(YdlRetrofitUtils.SERVER_COUPON_URL)
                .build().create(NetApiStore.class).getCourseCoupons(uid, services, receiveType, applyEnds);
    }

    //领取优惠券
    public static Observable<BaseResponse<List<String>>> receiveCoupon(String couponIds) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), couponIds);
        return YdlRetrofitUtils.getRxRetrofit().newBuilder().baseUrl(YdlRetrofitUtils.SERVER_COUPON_URL).build().create(NetApiStore.class).receiveCoupon(body);
    }

}
