package ai.pai.lensman.main;

import android.os.AsyncTask;
import android.support.annotation.StringRes;

import com.android.common.executors.ThreadExecutor;
import com.android.common.utils.LogHelper;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import ai.pai.lensman.base.BaseInteractor;
import ai.pai.lensman.utils.HttpPostTask;
import ai.pai.lensman.utils.UrlContainer;

/**
 * Created by chengzhenyu on 2017/4/4.
 */

public class SyncTimeInteractor implements BaseInteractor {
    private HttpPostTask timeSyncTask;
    private String lensmanId;
    private SyncTimeListener listener;
    private boolean isCancelled;

    public SyncTimeInteractor(String lensmanId, SyncTimeListener listener) {
        this.listener = listener;
        this.lensmanId = lensmanId;
    }

    @Override
    public void startJob() {
        cancelJob();
        isCancelled = false;
        HashMap<String, String> params = new HashMap<>();
        params.put("user_id", lensmanId);
        LogHelper.d("czy", "开始时间同步任务");
        timeSyncTask = new HttpPostTask(params) {

            @Override
            protected boolean parseResponse(String response) {
                if(isCancelled){
                    return false;
                }
                LogHelper.d("czy", "时间同步任务 服务器应答=" + response);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    JSONObject json = new JSONObject(response);
                    int status = json.getInt("status");
                    if (status == 200) {
                        JSONObject data = json.getJSONObject("data");
                        String server_time = data.getString("time");
                        Date serverDate = dateFormat.parse(server_time);
                        return Math.abs(serverDate.getTime()-new Date().getTime())<3600*1000;
                    }
                } catch (Exception e) {
                    LogHelper.d("czy", "时间同步任务 服务器应答异常 " + e);
                }
                return false;
            }

            @Override
            protected void onPostFail() {
                super.onPostFail();
                listener.onTimeSync(false, 0);
                LogHelper.d("czy", "时间同步失败");
            }

            @Override
            protected void onPostSuccess() {
                super.onPostSuccess();
                listener.onTimeSync(true, 0);
                LogHelper.d("czy", "时间同步成功");
            }
        };
        timeSyncTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.SERVER_TIME_URL);
    }

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

    public interface SyncTimeListener {

        void onTimeSync(boolean result, @StringRes int strId);
    }
}
