package ai.pai.lensman.session;

import android.content.Intent;

import com.android.common.utils.LogHelper;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import ai.pai.lensman.App;
import ai.pai.lensman.BuildConfig;
import ai.pai.lensman.bean.PhotoBean;
import ai.pai.lensman.bean.SessionBean;
import ai.pai.lensman.db.DBService;
import ai.pai.lensman.db.Preferences;
import ai.pai.lensman.main.QueryBoxStatusInteractor;
import ai.pai.lensman.service.UploadService;


public class SessionPresenter implements SessionContract.Presenter, SessionInteractor.SessionListener,
        QueryBoxStatusInteractor.BoxStatusListener {

    private SessionInteractor interactor;
    private QueryBoxStatusInteractor boxStatusInteractor;
    private ArrayList<PhotoBean> photoList;
    private SessionContract.View sessionView;
    private SessionBean sessionBean;
    private boolean isWorking;
    private String groupId;
    private Timer refreshTimer;

    private static final String TAG = "SessionPresenter";

    public SessionPresenter(SessionBean sessionBean, SessionContract.View view) {
        this.sessionView = view;
        this.sessionBean = sessionBean;
    }

    @Override
    public void start() {
        LogHelper.d(TAG,"SessionPresenter start "+sessionBean);
        interactor = new SessionInteractor(sessionBean, this);
        boxStatusInteractor = new QueryBoxStatusInteractor(this);
        photoList = DBService.getInstance().getPhotoListBySessionId(sessionBean.sessionId);
        if (photoList.size() == 0) {
            LogHelper.d(TAG,"SessionPresenter start and found no old photos");
            sessionView.showEmptyView();
        } else {
            LogHelper.d(TAG,"SessionPresenter start and found  old photos, size = "+photoList.size());
            sessionView.showPhotoRecyclerView();
            for (PhotoBean photoBean : photoList) {
                sessionView.addNewPhoto(photoBean);
            }
        }
        interactor.startSession();
        boxStatusInteractor.startJob();
        refreshTimer = new Timer();
        refreshTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                sessionView.refreshUploadTimeHint();
            }
        },1000,1000);
        Preferences.getInstance().setCurrentSession(sessionBean.sessionId);
    }

    @Override
    public void stop() {
        boxStatusInteractor.cancelJob();
        interactor.endSession();
        refreshTimer.cancel();
        Preferences.getInstance().setCurrentSession("");
        isWorking = false;
        LogHelper.d(TAG,"stop");
    }

    @Override
    public void onSessionStartSuccess(String session) {
        isWorking = true;
        sessionView.showToast("session启动成功");
        LogHelper.d(TAG,"onSessionStartSuccess session启动成功"+session);
    }

    @Override
    public void onSessionStartError(String session) {
        if (!isWorking) {
            return;
        }
        sessionView.showToast("session启动失败,自动重试中");
        interactor.startSession();
        LogHelper.d(TAG,"onSessionStartError session启动失败,自动重试中");
    }

    @Override
    public void onSessionPhotoCaptured(final PhotoBean bean) {
        if (!isWorking) {
            return;
        }
        if(BuildConfig.isTestMode){
            if(photoList!=null && photoList.size()>10){
                interactor.endSession();
                return;
            }
        }
        LogHelper.d(TAG,"onSessionPhotoCaptured "+bean);
        bean.groupId = groupId;
        DBService.getInstance().addPhotoBean(bean);
        sessionView.showPhotoRecyclerView();
        sessionView.addNewPhoto(bean);
        photoList.add(0,bean);
        App.getAppContext().startService(new Intent(App.getAppContext(), UploadService.class));
    }

    @Override
    public void onSessionEnd(String session) {
        LogHelper.d(TAG,"onSessionEnd "+session);
    }

    @Override
    public void swipeToDeletePhoto(int index) {
        if(index<0 || index>= photoList.size()){
            return;
        }
        PhotoBean photoBean = photoList.get(index);
        photoList.remove(photoBean);
        DBService.getInstance().deletePhotoBean(photoBean);
        interactor.deletePhoto(photoBean);
    }

    @Override
    public void change2QRCodeSessionId(String sessionId) {
        LogHelper.d(TAG,"change2QRCodeSessionId 使用新sessionId = "+sessionId);
        this.sessionBean.sessionId = sessionId;
        stop();
        start();
    }

    @Override
    public void addSessionPhotos2Group(String groupId) {
        this.groupId = groupId;
    }

    @Override
    public void onBoxStatusFetched(String boxNo, String boxStatus, int boxStatusCode) {
        LogHelper.d(TAG,"onBoxStatusFetched 盒子当前状态 "+boxStatus+" code = "+boxStatusCode);
        if(boxStatusCode == 200){
            sessionView.showBoxConnectedView();
        }else{
            sessionView.showBoxDisconnectedView(boxStatusCode,boxStatus);
        }
    }
}
