package ai.pai.lensman.session;

import android.content.Intent;

import com.android.common.utils.LogHelper;

import java.util.ArrayList;

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.service.UploadService;


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

    private SessionInteractor interactor;
    private ArrayList<PhotoBean> photoList;
    private SessionContract.View sessionView;
    private SessionBean sessionBean;
    private boolean isWorking;



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

    @Override
    public void start() {
        interactor = new SessionInteractor(sessionBean, this);
        photoList = DBService.getInstance().getPhotoListBySessionId(sessionBean.sessionId);
        if (photoList.size() == 0) {
            sessionView.showEmptyView();
        } else {
            sessionView.showPhotoRecyclerView();
            for (PhotoBean photoBean : photoList) {
                sessionView.addNewPhoto(photoBean);
            }
        }
        interactor.startSession();
        Preferences.getInstance().setCurrentSession(sessionBean.sessionId);
    }

    @Override
    public void stop() {
        interactor.endSession();
        Preferences.getInstance().setCurrentSession("");
        isWorking = false;
    }

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

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

    @Override
    public void onSessionPhotoCaptured(final PhotoBean bean) {
        if (!isWorking) {
            return;
        }
        if(BuildConfig.isTestMode){
            if(photoList!=null && photoList.size()>5){
                return;
            }
        }
        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) {

    }

    @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("czy","change2QRCodeSessionId ="+sessionId);
        this.sessionBean.sessionId = sessionId;
        stop();
        start();
    }

}
