package com.atmob.voiceai.utils;

import android.text.format.DateUtils;

import com.atmob.common.data.KVUtils;

public class ScoringUtils {


    public static final int KEY_SCORING_BURIAL = 10 * 60 * 1000;
    public static final String KEY_SCORING_SUCCESS = "scoring_success";
    public static final String KEY_SHOW_SCORING_TIMES_TOTAL = "show_scoring_times_total";
    public static final String KEY_SHOW_SCORING_TIME = "show_scoring_time";
    public static final String KEY_SHOW_SCORING_TIMES_TODAY = "show_scoring_times_today";

    private static final int KEY_SHOW_SCORING_TIMES_TOTAL_DEFAULT = 10;
    private static final int KEY_SHOW_SCORING_TIMES_TODAY_MAX = 2;


    public static void recordScoringShow() {
        int totalTimes = KVUtils.getDefault().getInt(KEY_SHOW_SCORING_TIMES_TOTAL, 0);
        KVUtils.getDefault().putInt(KEY_SHOW_SCORING_TIMES_TOTAL, totalTimes + 1);

        long lastShowTime = KVUtils.getDefault().getLong(KEY_SHOW_SCORING_TIME, 0);
        if (DateUtils.isToday(lastShowTime)) {
            int timesToday = KVUtils.getDefault().getInt(KEY_SHOW_SCORING_TIMES_TODAY, 0);
            KVUtils.getDefault().putInt(KEY_SHOW_SCORING_TIMES_TODAY, timesToday + 1);
        } else {
            KVUtils.getDefault().putInt(KEY_SHOW_SCORING_TIMES_TODAY, 1);
        }

        KVUtils.getDefault().putLong(KEY_SHOW_SCORING_TIME, System.currentTimeMillis());
    }

    public static boolean isHideScoring() {
        return KVUtils.getDefault().getBoolean(KEY_SCORING_SUCCESS, false);
    }

    public static void isScoringSuccess() {
        KVUtils.getDefault().putBoolean(KEY_SCORING_SUCCESS, true);
    }

    public static boolean checkShowScoring() {
        boolean scoringDone = KVUtils.getDefault().getBoolean(KEY_SCORING_SUCCESS, false);
        if (scoringDone) {
            return false;
        }
        int totalTimes = KVUtils.getDefault().getInt(KEY_SHOW_SCORING_TIMES_TOTAL, 0);
        if (totalTimes >= KEY_SHOW_SCORING_TIMES_TOTAL_DEFAULT) {
            return false;
        }
        int timesToday = KVUtils.getDefault().getInt(KEY_SHOW_SCORING_TIMES_TODAY, 0);
        if (timesToday > KEY_SHOW_SCORING_TIMES_TODAY_MAX) {
            return false;
        }
        long lastTime = KVUtils.getDefault().getLong(KEY_SHOW_SCORING_TIME, 0);
        if ((System.currentTimeMillis() - lastTime) < KEY_SCORING_BURIAL) {
            return false;
        }
        return true;
    }


}
