package com.hc.request.business;

import android.content.Context;
import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.hc.model.FJYD.AuthRequestBody;
import com.hc.model.FJYD.AuthResponse;
import com.hc.model.FJYD.AuthResponseUrl;
import com.hc.model.FJYD.FJYDContentProvider;
import com.hc.request.FJYDConfig;
import com.hc.request.core.CallServer;
import com.hc.request.core.DefaultCallback;
import com.hc.request.core.IHttpCallback;
import com.orhanobut.logger.Logger;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.StringRequest;

/**
 * 福建移动接口请求服务
 */
@Deprecated
public class FJYDService {

    private FJYDContentProvider contentProvider;

    public FJYDService(Context context) {
        contentProvider = new FJYDContentProvider(context);
    }

    /**
     * 认证请求
     *
     * @param cid     内容编号
     */
    public void auth(String cid, final IHttpCallback<AuthResponseUrl> callback) {
//        cid = "00000062201705271002120028799574";
        StringRequest request = getRequest(FJYDConfig.URL.AUTH);
        AuthRequestBody requestBody = new AuthRequestBody(cid);
        String body = JSON.toJSONString(requestBody);
        request.setDefineRequestBodyForJson(body);
        Logger.d("请求body %s", body);

        CallServer.getInstance().requestString(FJYDConfig.WHAT.AUTH, request, new DefaultCallback<String>() {
            @Override
            public void onStart(int what) {
                Logger.d("开始请求认证");
                if (callback != null) {
                    callback.onStart(what);
                }
            }

            @Override
            public void onSuccess(int what, @NonNull String data) {
                Logger.d("请求认证成功:" + data);
                AuthResponse response = JSON.parseObject(data, AuthResponse.class);
                if(response != null && response.success()) {
                    if(response.getUrls() != null && !response.getUrls().isEmpty()) {
                        callback.onSuccess(FJYDConfig.WHAT.AUTH, response.getUrls().get(0));
                    } else {
                        callback.onFail(FJYDConfig.WHAT.AUTH, "Url信息为空，无法获取到播放地址");
                    }
                } else {
                    if(response != null) {
                        callback.onFail(FJYDConfig.WHAT.AUTH, response.getDescription());
                    } else {
                        callback.onFail(FJYDConfig.WHAT.AUTH, "数据转换失败，无法获取到播放地址");
                    }
                }
            }

            @Override
            public void onFail(int what, String msg) {
                Logger.d("请求认证失败:" + msg);
                if (callback != null) {
                    callback.onFail(what, msg);
                }
            }

            @Override
            public void onError(int what, Throwable e) {
                Logger.e(e, "请求认证发生异常");
                if (callback != null) {
                    callback.onError(what, e);
                }
            }

            @Override
            public void onFinish(int what) {
                Logger.d("请求认证结束");
                if (callback != null) {
                    callback.onFinish(what);
                }
            }
        });
    }

    private StringRequest getRequest(String url) {
        StringRequest request = new StringRequest(contentProvider.getAuthority(true) + url, RequestMethod.POST);
        request.setContentType(Headers.HEAD_VALUE_CONTENT_TYPE_JSON);
        request.addHeader(FJYDConfig.HEADS.AUTHORIZATION, contentProvider.getToken());
        request.addHeader(FJYDConfig.HEADS.HOST, contentProvider.getAuthority(false));
        return request;
    }
}
