package ai.pai.lensman.upgrade;

import android.os.AsyncTask;
import android.text.TextUtils;

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

import org.json.JSONObject;

import java.util.HashMap;

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


public class CheckBoxUpgradeInfoInteractor implements BaseInteractor {
    private HttpPostTask upgradeTask;
    private String lensmanId;
    private FetchUpgradeInfoListener listener;

    private static final String TAG = "CheckBoxUpgradeInfoInteractor";
    
    public CheckBoxUpgradeInfoInteractor(String lensmanId, FetchUpgradeInfoListener listener) {
        this.listener = listener;
        this.lensmanId = lensmanId;
    }

    @Override
    public void startJob() {
        cancelJob();
        HashMap<String, String> params = new HashMap<>();
        params.put("user_id", lensmanId);
        LogHelper.d(TAG, "开始box升级任务");
        upgradeTask = new HttpPostTask(params) {

            String bpversion_str ="";
            
            @Override
            protected boolean parseResponse(String response) {
                LogHelper.d(TAG, "box升级任务 服务器应答=" + response);
                int latestVersionCode=0;
                try {
                    JSONObject json = new JSONObject(response);
                    int status = json.getInt("status");
                    if (status == 200) {
                        JSONObject data = json.getJSONObject("data");
                        bpversion_str = data.getString("bpversion_str");
                        latestVersionCode = data.getJSONObject("bpversion").getInt("srv_version_code");
                    }
                } catch (Exception e) {
                    LogHelper.d(TAG, "box升级任务 服务器应答异常 " + e);
                }
                LogHelper.d(TAG,"box升级任务 服务器最新版本="+latestVersionCode+"\t客户端版本="+Preferences.getInstance().getBoxVersionCode());
                if(latestVersionCode<= Preferences.getInstance().getBoxVersionCode()){
                    return false;
                }
                if(TextUtils.isEmpty(bpversion_str)){
                    return false;
                }
                return true;
            }

            @Override
            protected void onPostFail() {
                super.onPostFail();
                listener.onUpgradeInfoFetched(false, null);
                LogHelper.d(TAG, "box升级任务 获取更新失败或者无需更新");
            }

            @Override
            protected void onPostSuccess() {
                super.onPostSuccess();
                listener.onUpgradeInfoFetched(true, bpversion_str);
                LogHelper.d(TAG, "box升级任务 需要更新");
            }
        };
        upgradeTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.BOX_VERSION_URL);
    }

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

    public interface FetchUpgradeInfoListener {
        void onUpgradeInfoFetched(boolean result, String bpversion_str);
    }

}
