package com.duben.microtribe.mvp.presenters;

import android.content.Context;
import android.util.Log;

import com.duben.library.net.neterror.BaseSubscriber;
import com.duben.library.net.neterror.Throwable;
import com.duben.microtribe.MintsApplication;
import com.duben.microtribe.common.AppConfig;
import com.duben.microtribe.common.DeviceInfo;
import com.duben.microtribe.manager.AppHttpManager;
import com.duben.microtribe.manager.UserManager;
import com.duben.microtribe.mvp.model.BaseResponse;
import com.duben.microtribe.mvp.model.UserBean;
import com.duben.microtribe.utils.DeviceUuidFactory;
import com.google.gson.JsonObject;

import java.util.HashMap;

public class TrackPresenter extends BaseTrackPresenter {

    public void visitorlogin() {
        HashMap<String, Object> vo = new HashMap<>();
        vo.put("device", new DeviceUuidFactory().getDeviceUuid());

        saveV6Terminal(vo);

        AppHttpManager.getInstance(loanApplication)
                .call(loanService.visitorlogin(vo),
                        new BaseSubscriber<BaseResponse<UserBean>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onNext(BaseResponse<UserBean> baseResponse) {
                                switch (baseResponse.getStatus()) {
                                    case 200: {
                                        UserManager.getInstance().saveUserInfo(baseResponse.getData());

                                        firstShowVedio();

                                        saveTerminalInfo();
                                    }
                                }
                            }
                        });
    }

    public void getMyInfo() {
        HashMap<String, Object> vo = new HashMap<>();
        vo.put("os", "android");
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.getMyInfo(vo),
                        new BaseSubscriber<BaseResponse<UserBean>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onNext(BaseResponse<UserBean> baseResponse) {
                                switch (baseResponse.getStatus()) {
                                    case 200: {
                                        UserManager.getInstance().saveUserInfo(baseResponse.getData());
                                    }
                                }
                            }
                        });
    }

    public void saveTerminalInfo() {
        HashMap<String, Object> vo = new HashMap<>();
        DeviceInfo deviceInfo = DeviceInfo.Companion.getInstance();
        String macAddress = deviceInfo.getMacAddress();
        String mac = macAddress.replace(":", "");
        vo.put("androidid", deviceInfo.getAndroidId(null));
        vo.put("mac", mac);
        vo.put("mac1", macAddress);
        vo.put("os", "android");
        vo.put("model", deviceInfo.getBrand() + " " + deviceInfo.getMobileModel());
        vo.put("uuid", new DeviceUuidFactory().getDeviceUuid());
        vo.put("osversion", deviceInfo.getOSVersion());
        vo.put("appversion", deviceInfo.getVersionName());
        vo.put("oaid", MintsApplication.OAID);
        vo.put("imei", deviceInfo.getIMEI());
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.saveTerminalInfo(vo),
                        new BaseSubscriber<BaseResponse<Object>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BaseResponse<Object> baseResponse) {

                                switch (baseResponse.getStatus()) {
                                    case 200: {
                                        getMyInfo();
                                    }
                                }
                            }
                        });
    }


    public void commitVedio(HashMap<String, Object> vo) {
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.reportIndex(vo),
                        new BaseSubscriber<BaseResponse<Object>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BaseResponse<Object> baseResponse) {
                            }
                        });
    }

    public void saveV6Terminal(HashMap<String, Object> vo) {
        try {
            Context context = MintsApplication.getContext();
            MintsApplication baseApplication = (MintsApplication) context;
            AppHttpManager.getInstance(baseApplication)
                    .call(baseApplication.getV6Service().saveV6TerminalInfoBefore(vo),
                            new BaseSubscriber<BaseResponse<Object>>() {
                                @Override
                                public void onCompleted() {
                                }

                                @Override
                                public void onError(Throwable e) {
                                }

                                @Override
                                public void onNext(BaseResponse<Object> baseResponse) {
                                }
                            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void collect(String vedioId) {
        HashMap vo = new HashMap<String, Object>();
        vo.put("vedioId", vedioId);
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.collect(vo), new BaseSubscriber<BaseResponse<JsonObject>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });

    }

    public void cancelCollect(String vedioId) {
        HashMap vo = new HashMap<String, Object>();
        vo.put("vedioId", vedioId);
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.cancelCollect(vo), new BaseSubscriber<BaseResponse<JsonObject>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });

    }

    public void commitCsjShotInfo(String json) {
        HashMap<String, Object> vo = new HashMap<>();
        vo.put("list", json);
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.commitCsjShotInfo(vo),
                        new BaseSubscriber<BaseResponse<Object>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onNext(BaseResponse<Object> baseResponse) {
                                switch (baseResponse.getStatus()) {
                                    case 200: {
                                        Log.d("AboutusActivity", "upload service onNext success");

                                    }
                                }
                            }
                        });
    }


    public void firstShowVedio() {
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.firstShowVedio(),
                        new BaseSubscriber<BaseResponse<JsonObject>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onNext(BaseResponse<JsonObject> baseResponse) {
                                switch (baseResponse.getStatus()) {
                                    case 200: {
                                        try {
                                            JsonObject data = baseResponse.getData();
                                            AppConfig.showVipVedio = data.get("canShowVedio4").getAsBoolean();
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }
                        });

    }
}
