package uprrp.tania.models;

import android.util.Log;

public class UserStatusModel {

    private static final String TAG = "UserStatusModel";
    // NOTE: these attributes must have the same name
    // as in the JSON and need not be manually initialized
    private String institucion;
    private String experiencia;
    private int surveys_completed;
    private int surveys_sent;
    private String time_to_next;
    private boolean available_now;

    public boolean isEnrolled() {
        return true;
    }

    public boolean surveyAvailable() {
        return this.available_now;
    }

    public boolean ranOutOfMoments() {
        // NOTE: null signifies experience has ran out of moments
        return this.time_to_next == null;
    }

    public String getTimeToNext() {
        if(this.ranOutOfMoments()) {
            return "No more surveys for now.\nCome back later!";
        } else if(this.surveyAvailable()) {
            return "Survey available!";
        } else {
            return "Next survey in " + this.formatTimeToNext() + ".";
        }
    }

    private boolean isInteger(String s) {
        return s.matches("-?\\d+");
    }

    private boolean validTimeToNext() {
        // Format is DD:HH:MM (can be negative if survey can be answered)
        String[] parts = this.time_to_next.split(":");

        if(parts.length != 3) {
            return false;
        } else if(!isInteger(parts[0]) || !isInteger(parts[1]) || !isInteger(parts[2])) {
            return false;
        } else {
            return true;
        }
    }

    private String formatTimeToNext() {

        // Validation
        if(!this.validTimeToNext()) {
            Log.wtf(TAG, "Backend gave an invalid time_to_next: " + this.time_to_next);
            return ".."; // forms ellipsis
        }

        // Format is DD:HH:MM (can be negative if survey can be answered)
        String[] parts = this.time_to_next.split(":");
        int days = Integer.parseInt(parts[0]);
        int hours = Integer.parseInt(parts[1]);
        int minutes = Integer.parseInt(parts[2]);
        String formattedTimeToNext = "";

        if(days != 0) {
            formattedTimeToNext += days + " days";
            if(hours != 0) {
                formattedTimeToNext += ", " + hours + " hours";
            } else {
                formattedTimeToNext += ", " + minutes + " minutes";
            }
        } else {
            if(hours != 0) {
                formattedTimeToNext += hours + " hours";
                formattedTimeToNext += ", " + minutes + " minutes";
            } else {
                if(minutes != 0) {
                    formattedTimeToNext += minutes + " minutes";
                } else {
                    Log.wtf(TAG, "Encountered a 0:0:0 in UserStatusModel.formatTimeToNext()!\nShould've been handled by UserStatusModel.surveyAvailable()");
                }
            }
        }

        return formattedTimeToNext;

    }

    public String toString() {

        // If user is not enrolled in an experience,
        // return early with this string
        if(!this.isEnrolled()) {
            return "You are not enrolled in any experience.\nIf you think this is a mistake, contact your supervisors.";
        }

        // Else, return proper string
        String status = "You're enrolled in " + this.experiencia + ", by " + this.institucion + ".";
        if(this.surveys_completed == this.surveys_sent) {
            status += " You've answered all surveys. Come back later in case a new one pops up!";
        } else {
            status += " So far, you have completed " + this.surveys_completed + " out of " + this.surveys_sent + " delivered surveys.";
        }
        return status;

    }

}
