package org.tndata.android.compass.model;

import android.os.Parcel;
import android.support.annotation.NonNull;

import com.google.gson.annotations.SerializedName;

import java.util.Calendar;
import java.util.Date;


/**
 * Model superclass for anything that can be classified as an action.
 *
 * @author Ismael Alonso
 * @version 1.0.0
 */
public abstract class Action extends UserContent implements Comparable<Action>{
    @SerializedName("trigger")
    private Trigger mTrigger;
    @SerializedName("next_reminder")
    private String mNextReminder;


    protected Action(){

    }

    public void setTrigger(Trigger trigger){
        mTrigger = trigger;
    }

    public Trigger getTrigger(){
        return mTrigger != null ? mTrigger : new Trigger();
    }

    public boolean hasTrigger(){
        return mTrigger != null;
    }

    public boolean isTriggerEnabled(){
        return mTrigger.isEnabled();
    }

    public void setNextReminder(String nextReminder){
        mNextReminder = nextReminder;
    }

    public String getNextReminder(){
        return mNextReminder != null ? mNextReminder : "";
    }

    public Date getNextReminderDate(){
        String year = mNextReminder.substring(0, mNextReminder.indexOf("-"));
        String temp = mNextReminder.substring(mNextReminder.indexOf("-")+1);
        String month = temp.substring(0, temp.indexOf("-"));
        temp = temp.substring(temp.indexOf("-")+1);
        String day = temp.substring(0, temp.indexOf("T"));

        String time = mNextReminder.substring(mNextReminder.indexOf('T')+1);
        String hour = time.substring(0, time.indexOf(':'));
        time = time.substring(time.indexOf(':')+1);
        String minute = time.substring(0, time.indexOf(':'));

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.valueOf(year));
        calendar.set(Calendar.MONTH, Integer.valueOf(month)-1);
        calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(day));
        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour));
        calendar.set(Calendar.MINUTE, Integer.valueOf(minute));
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public String getNextReminderDisplay(){
        if (mNextReminder == null){
            return "";
        }

        String time = mNextReminder.substring(mNextReminder.indexOf('T')+1);
        String hourStr = time.substring(0, time.indexOf(':'));
        time = time.substring(time.indexOf(':')+1);
        try{
            boolean am = true;
            int hour = Integer.valueOf(hourStr);
            if (hour > 12){
                hour -= 12;
                am = false;
            }

            return hour + ":" + time.substring(0, time.indexOf(":")) + (am ? " am" : " pm");
        }
        catch (NumberFormatException nfx){
            nfx.printStackTrace();
            return "";
        }
    }

    @Override
    public int compareTo(@NonNull Action another){
        if (mTrigger == null && another.mTrigger == null){
            return getTitle().compareTo(another.getTitle());
        }
        else if (mTrigger == null){
            return 1;
        }
        else if (another.mTrigger == null){
            return -1;
        }
        else{
            int trigger = mTrigger.compareTo(another.mTrigger);
            if (trigger == 0){
                return getTitle().compareTo(another.getTitle());
            }
            else{
                return trigger;
            }
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags){
        super.writeToParcel(dest, flags);
        dest.writeParcelable(getTrigger(), flags);
        dest.writeString(getNextReminder());
    }

    protected Action(Parcel src){
        super(src);
        mTrigger = src.readParcelable(Trigger.class.getClassLoader());
        mNextReminder = src.readString();
    }

    public abstract String getTitle();
}

@@ -920,107 +920,8 @@
  %7D%0A%0A
-    public void setNextReminder(String nextReminder)%7B%0A        mNextReminder = nextReminder;%0A    %7D%0A%0A
     
@@ -1379,17 +1379,17 @@
 ndexOf(%22
-T
+ 
 %22));%0A%0A  
@@ -1447,33 +1447,33 @@
 minder.indexOf('
-T
+ 
 ')+1);%0A        S
@@ -2147,727 +2147,8 @@
  %7D%0A%0A
-    public String getNextReminderDisplay()%7B%0A        if (mNextReminder == null)%7B%0A            return %22%22;%0A        %7D%0A%0A        String time = mNextReminder.substring(mNextReminder.indexOf('T')+1);%0A        String hourStr = time.substring(0, time.indexOf(':'));%0A        time = time.substring(time.indexOf(':')+1);%0A        try%7B%0A            boolean am = true;%0A            int hour = Integer.valueOf(hourStr);%0A            if (hour %3E 12)%7B%0A                hour -= 12;%0A                am = false;%0A            %7D%0A%0A            return hour + %22:%22 + time.substring(0, time.indexOf(%22:%22)) + (am ? %22 am%22 : %22 pm%22);%0A        %7D%0A        catch (NumberFormatException nfx)%7B%0A            nfx.printStackTrace();%0A            return %22%22;%0A        %7D%0A    %7D%0A%0A
     

Fix ActionActivity back crash and remove unnecessary code