package com.casic.dcms.vm;

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

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.base.BaseApplication;
import com.casic.dcms.model.ActionResultBean;
import com.casic.dcms.utils.ImageHelper;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.callback.IWaterMarkAddListener;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pengxh.androidx.lite.callback.OnObserverCallback;
import com.pengxh.androidx.lite.hub.StringHub;
import com.pengxh.androidx.lite.vm.BaseViewModel;
import com.pengxh.androidx.lite.vm.LoadState;
import com.pengxh.androidx.lite.vm.ObserverSubscriber;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;

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(String imei, String positionLat, String positionLng) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getPositionUploadResult(imei, positionLat, positionLng);
        ObserverSubscriber.addSubscribe(observable, new 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.separateResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        commonActionModel.setValue(resultBean);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void uploadImage(File file) {
        loadState.setValue(LoadState.Loading);
        //改为上传带水印的图片
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        ImageHelper.drawTextToRightBottom(bitmap, new IWaterMarkAddListener() {
            @Override
            public void onSuccess(File file) {
                if (file != null) {
                    Observable<ResponseBody> observable = RetrofitServiceManager.uploadImage(file);
                    ObserverSubscriber.addSubscribe(observable, new 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.separateResponseCode(response);
                                if (responseCode == 200) {
                                    ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                                    }.getType());
                                    imageModel.setValue(resultBean);
                                } else {
                                    loadState.setValue(LoadState.Fail);
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    loadState.setValue(LoadState.Fail);
                }
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }
        });
    }

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

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

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

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

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

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