package com.duben.happyplaylet.mvp.presenters;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.google.gson.JsonObject;
import com.duben.happyplaylet.common.Constant;
import com.duben.happyplaylet.manager.LocalVedioManager;
import com.duben.happyplaylet.mvp.model.IndexList;
import com.duben.happyplaylet.mvp.model.VedioBean;
import com.duben.happyplaylet.utils.AppPreferencesManager;
import com.duben.library.net.neterror.BaseSubscriber;
import com.duben.library.net.neterror.Throwable;
import com.duben.happyplaylet.MintsApplication;
import com.duben.happyplaylet.common.DeviceInfo;
import com.duben.happyplaylet.manager.AppHttpManager;
import com.duben.happyplaylet.manager.UserManager;
import com.duben.happyplaylet.mvp.model.BaseResponse;
import com.duben.happyplaylet.mvp.model.UserBean;
import com.duben.happyplaylet.utils.DeviceUuidFactory;

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());

                                        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");

                                    }
                                }
                            }
                        });
    }

}
