package com.android.chmo.http.service;

import com.android.chmo.app.ChmoApplication;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.model.LoginUser;
import com.android.chmo.model.ModelOrder;
import com.android.chmo.utils.DateUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.util.Date;

/**
 * Created by Administrator on 2018/8/4.
 */

public class OrderService {
    private static RequestParams getRequestParams() {
        RequestParams params = new RequestParams(HttpApi.BASE_URL + "order");
        return params;
    }

    /**
     * 提交订单
     */
    public static Callback.Cancelable submitOrder(ModelOrder order, String workName, String contact, String phone, String workAddr, String mark, RequestCallback callback) {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "doPutOrder");
        params.addQueryStringParameter("PK", user.pk);
        params.addQueryStringParameter("ModelPK", order.model.modelpk);
        params.addQueryStringParameter("BrokerPK", order.model.brokepk);
        params.addQueryStringParameter("LederPK", "0");
        params.addQueryStringParameter("ODate", DateUtils.formatHourDate(order.startTime));
        params.addQueryStringParameter("SDate", DateUtils.formatHourDate(order.endTime));
        params.addQueryStringParameter("SCount", "" + order.hour);
        params.addQueryStringParameter("SName", workName);
        params.addQueryStringParameter("SPrice", order.skill.price);
        params.addQueryStringParameter("coin", "" + String.valueOf(order.totalPrice));
        params.addQueryStringParameter("OrderState", "0");
        params.addQueryStringParameter("intro", mark);
        params.addQueryStringParameter("skillpk", order.skill.skillpk);
        params.addQueryStringParameter("linkman", contact);
        params.addQueryStringParameter("linkphone", phone);
        params.addQueryStringParameter("addr", workAddr);
        return HttpApi.GET(params, callback);
    }


    public static Callback.Cancelable getOrder(String orderPk, RequestCallback callback) {
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "doGetOrderInfo");
        params.addQueryStringParameter("orderpk", orderPk);
        return HttpApi.GET(params, callback);
    }

    public static Callback.Cancelable payOrder(String orderPk, RequestCallback callback) {
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "domemberpayorder");
        params.addQueryStringParameter("orderpk", orderPk);
        return HttpApi.GET(params, callback);
    }

    public static Callback.Cancelable cancelOrder(String orderPk, RequestCallback callback) {
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "domembercancelorder");
        params.addQueryStringParameter("orderpk", orderPk);
        return HttpApi.GET(params, callback);
    }


    public static Callback.Cancelable getOrderList(String state, int page, RequestCallback callback) {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "doGetOrder");
        params.addQueryStringParameter("pk", user.pk);
        params.addQueryStringParameter("state", state);
        params.addQueryStringParameter("index", page + "");
        params.addQueryStringParameter("size", Constants.PAGE_SIZE + "");
        return HttpApi.GET(params, callback);
    }

    public static Callback.Cancelable getVideoOrder(int page, RequestCallback callback) {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "getMemberVideoRecordOrder");
        params.addQueryStringParameter("memberpk", user.pk);
        params.addQueryStringParameter("index", page + "");
        params.addQueryStringParameter("size", Constants.PAGE_SIZE + "");
        return HttpApi.GET(params, callback);
    }


    public static Callback.Cancelable beginVChat(String modelPk, RequestCallback callback) {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "dobegvchat");
        params.addQueryStringParameter("pk", user.pk);
        params.addQueryStringParameter("modelpk", modelPk);
        params.addQueryStringParameter("begdate", DateUtils.formatDateTime(new Date()));
        params.addQueryStringParameter("hasanswer", "1");
        return HttpApi.GET(params, callback);
    }

    public static Callback.Cancelable answerVChat(String modelPk, RequestCallback callback) {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "doansvchat");
        params.addQueryStringParameter("videopk", "0");
        params.addQueryStringParameter("pk", user.pk);
        params.addQueryStringParameter("modelpk", modelPk);
        params.addQueryStringParameter("begdate", DateUtils.formatDateTime(new Date()));
        params.addQueryStringParameter("hasanswer", "1");
        return HttpApi.GET(params, callback);
    }

    public static Callback.Cancelable endVChat(String videoPk, int duration, RequestCallback callback) {
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "doendvchat");
        params.addQueryStringParameter("videopk", videoPk);
        params.addQueryStringParameter("duration", "" + duration);
        return HttpApi.GET(params, callback);
    }

    public static Callback.Cancelable checkVChat(String videoPk, int duration, RequestCallback callback) {
        RequestParams params = getRequestParams();
        params.addQueryStringParameter("action", "dovchattest");
        params.addQueryStringParameter("videopk", videoPk);
        params.addQueryStringParameter("duration", "" + duration);
        return HttpApi.GET(params, callback);
    }


    /**
     * 支付宝提交订单
     *
     * @return
     */
    public static Callback.Cancelable alipay(float money, RequestCallback callback) {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        RequestParams params = new RequestParams(HttpApi.BASE_URL + "pay");
        params.addQueryStringParameter("action", "alipay");
        params.addQueryStringParameter("memberpk", user.pk);
        params.addQueryStringParameter("totalamount", "" + money);
        params.addQueryStringParameter("timeoutexpress", "3m");
        params.addQueryStringParameter("body", "账户充值");
        params.addQueryStringParameter("subject", "充值金额" + money);
        return HttpApi.GET(params, callback);
    }


    /**
     * 支付宝成功回调
     */
    public static Callback.Cancelable alipayResult(String payPk, String outtradeno, int result, RequestCallback callback) {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        RequestParams params = new RequestParams(HttpApi.BASE_URL + "pay");
        params.addQueryStringParameter("action", "alipaycallback");
        params.addQueryStringParameter("pk", payPk);
        params.addQueryStringParameter("outtradeno", outtradeno);
        params.addQueryStringParameter("result", result == 1 ? "success" : "fail");
        return HttpApi.GET(params, callback);
    }
}
