package ai.pai.lensman.session;

import com.android.common.utils.LogHelper;

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

import ai.pai.lensman.BuildConfig;
import ai.pai.lensman.bean.PhotoBean;
import ai.pai.lensman.bean.SessionBean;
import ai.pai.lensman.db.DBService;

public class SessionInteractor {

    private SessionBean sessionBean;
    private Timer timer;
    private SessionListener listener;

    private long maxId = 0;
    private boolean isWorking;
    private boolean isLastQueryReturned = true;
    private static final String TAG = "SessionInteractor";

        public SessionInteractor(SessionBean sessionBean, SessionListener listener) {
        this.listener = listener;
        this.sessionBean = sessionBean;
    }

    public interface SessionListener {

        void onSessionStartSuccess(String session);
        void onSessionStartError(String session);
        void onSessionPhotoCaptured(PhotoBean bean);
        void onSessionEnd(String session);
    }

    public void startSession(){
        LogHelper.d(TAG,"startSession");

        ArrayList<PhotoBean> photoList = DBService.getInstance().getPhotoListBySessionId(sessionBean.sessionId);
        if(photoList!=null && photoList.size()>0){
            for(PhotoBean photoBean : photoList){
                if(photoBean.photoId>maxId){
                    maxId = photoBean.photoId;
                }
            }
        }

        listener.onSessionStartSuccess(sessionBean.sessionId);
        isWorking = true;
        startCapture();

        LogHelper.d(TAG,"startSession sessionStartTask execute");
    }

    public void startCapture() {
        if(timer!=null){
            timer.cancel();
            timer = null;
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                fetchPhotoTask();
            }
        },5000,2000);
    }

    private void fetchPhotoTask(){
        if(!isWorking){
            return;
        }
        if(!isLastQueryReturned){
            return;
        }

        if(BuildConfig.isTestMode){
            PhotoBean bean = new PhotoBean();
            long milisec = System.currentTimeMillis();
            bean.photoName = milisec+".jpg";
            bean.photoId = milisec;
            if(milisec%5==0){
                bean.photoPath = "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1408/11/c0/37303155_1407746429945_800x600.jpg";
            }else if(milisec%5==1){
                bean.photoPath ="http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1408/07/c0/37179063_1407421362265_800x600.jpg";
            }else if(milisec%5==2){
                bean.photoPath ="http://h.hiphotos.baidu.com/zhidao/pic/item/6d81800a19d8bc3ed69473cb848ba61ea8d34516.jpg";
            }else if(milisec%5==3){
                bean.photoPath ="http://img.boqiicdn.com/Data/BK/A/1607/25/imagick97881469425045_y.jpg";
            }else{
                bean.photoPath = "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1408/11/c0/37303160_1407746433139_800x600.jpg";
            }
            bean.captureTime = bean.photoId;
            bean.isRawPhoto = false;
            bean.uploadStatus = PhotoBean.UploadStatus.STATUS_NO_BEGIN;
            bean.sessionId = sessionBean.sessionId;
            bean.lensmanId = sessionBean.lensmanId;
            bean.sessionSeq = sessionBean.sessionSeq;
            bean.sessionDate = sessionBean.sessionDate;
            bean.sessionCreateTime = sessionBean.createTime;
            //TODO
            return;
        }

        isLastQueryReturned = false;
        LogHelper.d(TAG,"fetchPhotoTask starts");
    }



    public void endSession(){
        isWorking = false;
        if(timer!=null){
            timer.cancel();
            timer = null;
        }
        //TODO
        listener.onSessionEnd(sessionBean.sessionId);
    }

    public void deletePhoto(PhotoBean photoBean){
        //TODO
        LogHelper.d(TAG,"deletePhoto execute "+photoBean);
    }


}
