package tech.starwin.mvp.presenter;

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

import com.google.gson.Gson;


import java.io.File;

import tech.starwin.base.BasePresenter;
import tech.starwin.constants.TrackEvent;
import tech.starwin.impl.HttpObserver;
import tech.starwin.mvp.api.UploadApi;
import tech.starwin.mvp.beans.BasicAck;
import tech.starwin.mvp.beans.OcrResultBean;
import tech.starwin.utils.GeneralUtils;
import tech.starwin.utils.LoginManager;
import tech.starwin.utils.MultipartBodyMaker;
import tech.starwin.utils.PreferencesManager;
import tech.starwin.utils.TrackEventHelper;
import tech.starwin.utils.context_utils.AppInfoUtils;

/**
 * Created by SiKang on 2018/9/25.
 */
public class UploadPresenter extends BasePresenter<UploadApi> {

    public UploadPresenter() {
        super(UploadApi.class);
    }

    /**
     * 身份证ocr识别
     */
    public void identityOcr(final String action, @NonNull File file) {
        handleRequest(apiService.identityOcr(MultipartBodyMaker.makeSimplePart("file", file), LoginManager.get().getToken()),
                new HttpObserver<OcrResultBean>() {
                    @Override
                    public void onStart() {
                        view.onHttpStart(action, true);
                    }

                    @Override
                    public void onSuccess(OcrResultBean data) {
                        TrackEventHelper.logEvent(TrackEvent.IDENTITY_INFO_SUBMIT);
                        OcrResultBean.KTP ktp = new Gson().fromJson(data.getData(), OcrResultBean.KTP.class);
                        if (ktp != null && ktp.getResult() != null) {
                            if (ktp.getResult().equals("PASS")) {
                                view.onHttpSuccess(action, ktp);
                            } else {
                                view.onHttpError(action, ktp.getMessage());
                            }
                        } else {
                            view.onHttpError(action, data.getMessage());
                        }
                    }

                    @Override
                    public void onError(int code, String msg) {
                        view.onHttpError(action, msg);
                    }

                    @Override
                    public void onFinish() {
                        view.onHttpFinish(action);
                    }
                });
    }


    /**
     * 上传依图 截取得帧图片
     */
    public void faceVerify(final String action, Context context, double amount, int day, long productId) {
        TrackEventHelper.logEvent(TrackEvent.FACE_DETECTION);
        handleRequest(
                apiService.faceVerify(
                        "PAYDAY",
                        amount,
                        day,
                        "D",
                        null,
                        null,
                        MultipartBodyMaker.makeSimplePart("delta", PreferencesManager.get().getVerificationData()),
                        AppInfoUtils.getAndroidID(context),
                        productId,
                        LoginManager.get().getToken()
                ),
                new HttpObserver<BasicAck>() {
                    @Override
                    public void onStart() {
                        view.onHttpStart(action, true);
                    }

                    @Override
                    public void onSuccess(BasicAck data) {
                        TrackEventHelper.logEvent(TrackEvent.FACE_DETECTION_SUCCESS);
                        view.onHttpSuccess(action, data);
                    }

                    @Override
                    public void onError(int code, String msg) {
                        view.onHttpError(action, msg);
                    }

                    @Override
                    public void onFinish() {
                        view.onHttpFinish(action);
                    }
                });
    }
}
