package ai.pai.lensman.main;


import android.os.AsyncTask;

import com.android.common.executors.ThreadExecutor;

import org.json.JSONObject;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import ai.pai.lensman.BuildConfig;
import ai.pai.lensman.base.BaseInteractor;
import ai.pai.lensman.utils.BoxUrlContainer;
import ai.pai.lensman.utils.HttpPostTask;

public class QueryBoxStatusInteractor  implements BaseInteractor{
    
    private HttpPostTask queryBoxTask;
    private BoxStatusListener listener;
    private Timer queryTimer;
    private boolean isCancelled;

    public QueryBoxStatusInteractor(BoxStatusListener listener){
        this.listener = listener;
    }

    @Override
    public void startJob() {
        cancelJob();
        queryTimer = new Timer();
        queryTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                queryBoxStatus();
            }
        },100,6000);

    }

    private void queryBoxStatus(){
        queryBoxTask = new HttpPostTask(null){

            int boxStatusNum;
            String boxStatusMsg;
            String boxNo;
            @Override
            protected boolean parseResponse(String response) {
                try{
                    JSONObject json = new JSONObject(response);
                    int status = json.getInt("status");
                    if(status == 200){
                        JSONObject data = json.getJSONObject("data");
                        boxNo = data.getString("no.");
                        JSONObject boxStatus = data.getJSONObject("status");
                        boxStatusMsg = boxStatus.getString("msg");
                        boxStatusNum = boxStatus.getInt("code");
                        return true;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            protected void onPostFail() {
                super.onPostFail();
                if(isCancelled){
                    return;
                }
                if(BuildConfig.isTestMode){
                    if(listener!=null){
                        listener.onBoxStatusFetched("123","状态消息",100*new Random().nextInt(3)+new Random().nextInt(3));
                    }
                    return;
                }
            }

            @Override
            protected void onPostSuccess() {
                super.onPostSuccess();
                if(isCancelled){
                    return;
                }
                if(listener!=null){
                    listener.onBoxStatusFetched(boxNo,boxStatusMsg,boxStatusNum);
                }
            }
        };
        queryBoxTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), BoxUrlContainer.BOX_INFO_URL);
    }

    @Override
    public void cancelJob() {
        isCancelled = true;
        if(queryTimer!=null){
            queryTimer.cancel();
            queryTimer = null;
        }
        if(queryBoxTask==null){
            return;
        }
        if(queryBoxTask.getStatus()== AsyncTask.Status.RUNNING){
            queryBoxTask.cancel(true);
        }
        queryBoxTask = null;
    }

    public interface BoxStatusListener{

        void onBoxStatusFetched(String boxNo, String boxStatus, int boxStatusCode);
    }
}
