package com.casic.dcms.vm;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.ActionResultBean;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pengxh.androidx.lite.annotations.WaterMarkPosition;
import com.pengxh.androidx.lite.base.BaseViewModel;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.FloatKit;
import com.pengxh.androidx.lite.kit.LongKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.ObserverSubscriber;
import com.pengxh.androidx.lite.utils.WaterMarkerEngine;

import java.io.File;
import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Observable;

public class UploadViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<ActionResultBean> commonActionModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> imageModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> audioModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> videoModel = new MutableLiveData<>();

    public void uploadPosition(Context context, String imei, String positionLat, String positionLng) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getPositionUploadResult(imei, positionLat, positionLng);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        commonActionModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void uploadImage(Context context, File file) {
        File compressImageDir = ContextKit.createCompressImageDir(context);
        //改为上传带水印的图片
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        new WaterMarkerEngine.Builder()
                .setOriginalBitmap(bitmap)
                .setTextMaker(LongKit.timestampToCompleteDate(System.currentTimeMillis()) + System.lineSeparator() + StringHelper.getUserBean().getAccount())
                .setTextColor(Color.RED)
                .setTextSize(FloatKit.sp2px(context, 24))
                .setTextMargin(FloatKit.dp2px(context, 30))
                .setMarkerPosition(WaterMarkPosition.RIGHT_BOTTOM)
                .setMarkedSavePath(compressImageDir + File.separator + System.currentTimeMillis() + ".png")
                .setOnWaterMarkerAddedListener(new WaterMarkerEngine.OnWaterMarkerAddedListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onMarkAdded(File file) {
                        loadState.setValue(LoadState.Loading);
                        Observable<ResponseBody> observable = RetrofitServiceManager.uploadImage(file);
                        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
                            @Override
                            public void onCompleted() {
                                loadState.setValue(LoadState.Success);
                            }

                            @Override
                            public void onError(Throwable e) {
                                loadState.setValue(LoadState.Fail);
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    String response = responseBody.string();
                                    int responseCode = StringHelper.getResponseCode(response);
                                    if (responseCode == 200) {
                                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                                        }.getType());
                                        imageModel.setValue(resultBean);
                                        loadState.setValue(LoadState.Success);
                                    } else {
                                        loadState.setValue(LoadState.Fail);
                                        StringKit.show(context, StringHelper.getResponseMessage(response));
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }).build().start();
    }

    public void uploadAudio(Context context, File file) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.uploadAudio(file);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        audioModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void uploadVideo(Context context, File file) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.uploadVideo(file);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        videoModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}