package com.casic.dcms.mvp.presenter.impl;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.casic.dcms.bean.ActionResultBean;
import com.casic.dcms.listener.OnActionResultListener;
import com.casic.dcms.mvp.BasePresenter;
import com.casic.dcms.mvp.model.impl.UploadFileModelImpl;
import com.casic.dcms.mvp.presenter.IUploadFilePresenter;
import com.casic.dcms.mvp.view.IUploadFileView;
import com.casic.dcms.utils.ImageHelper;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.casic.dcms.utils.callback.IWaterMarkAddListener;

import java.io.File;

public class UploadImagePresenterImpl extends BasePresenter implements IUploadFilePresenter, OnActionResultListener {

    private final IUploadFileView view;
    private final UploadFileModelImpl actionModel;

    public UploadImagePresenterImpl(IUploadFileView uploadImageView) {
        this.view = uploadImageView;
        actionModel = new UploadFileModelImpl(this);
    }

    @Override
    public void onReadyRetrofitRequest(File file) {
        view.showProgress();
        //改为上传带水印的图片
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        ImageHelper.drawTextToRightBottom(bitmap, StringHelper.getUserAccount(),
                TimeOrDateUtil.timestampToDate(System.currentTimeMillis()),
                TimeOrDateUtil.timestampToTime(System.currentTimeMillis()),
                new IWaterMarkAddListener() {
                    @Override
                    public void onSuccess(File file) {
                        if (file != null) {
                            addSubscription(actionModel.sendUploadFileRequest(file));
                        } else {
                            view.obtainDataFail();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.obtainDataFail();
                    }
                });
    }

    @Override
    public void onSuccess(ActionResultBean resultBean) {
        view.hideProgress();
        view.obtainUploadImageResult(resultBean);
    }

    @Override
    public void onFailure(Throwable throwable) {
        view.hideProgress();
        view.obtainDataFail();
    }

    @Override
    public void disposeRetrofitRequest() {
        unSubscription();
    }
}
