package nl.rmokveld.castnotifications;

import android.content.ContentValues;
import android.database.Cursor;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;

import com.google.android.gms.cast.MediaInfo;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class CastNotification {

    public static final int STATE_NORMAL = 0;
    public static final int STATE_CONNECTING = 1;
    public static final String TABLE_NAME = "Notifications";
    static final String COL_ID = "id";
    private static final String COL_TITLE = "title";
    private static final String COL_TEXT = "text";
    private static final String COL_MEDIA_INFO = "media_info";
    static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
            COL_ID + " INTEGER PRIMARY KEY," +
            COL_TITLE + " TEXT," +
            COL_TEXT + " TEXT," +
            COL_MEDIA_INFO + " TEXT);";

    private final int mId;
    private final String mTitle, mContentText;
    private final MediaInfo mMediaInfo;
    @State private int mState = STATE_NORMAL;
    private String mDeviceName;

    public CastNotification(int id, String title, String contentText, @NonNull MediaInfo mediaInfo) {
        mId = id;
        mTitle = title;
        mContentText = contentText;
        mMediaInfo = mediaInfo;
    }
    public CastNotification(Cursor cursor) {
        mId = cursor.getInt(cursor.getColumnIndexOrThrow(COL_ID));
        mTitle = cursor.getString(cursor.getColumnIndexOrThrow(COL_TITLE));
        mContentText = cursor.getString(cursor.getColumnIndexOrThrow(COL_TEXT));
        mMediaInfo = CastNotificationManager.getInstance().getMediaInfoSerializer().toMediaInfo(cursor.getString(cursor.getColumnIndexOrThrow(COL_MEDIA_INFO)));
    }

    public String getTitle() {
        return mTitle;
    }

    public String getContentText() {
        return mContentText;
    }

    public MediaInfo getMediaInfo() {
        return mMediaInfo;
    }

    public int getId() {
        return mId;
    }

    public void setState(@State int state, String deviceName) {
        mState = state;
        mDeviceName = deviceName;
    }

    @State
    public int getState() {
        return mState;
    }

    public String getDeviceName() {
        return mDeviceName;
    }

    public ContentValues toContentValues() {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_ID, mId);
        contentValues.put(COL_TITLE, mTitle);
        contentValues.put(COL_TEXT, mContentText);
        contentValues.put(COL_MEDIA_INFO, CastNotificationManager.getInstance().getMediaInfoSerializer().toJson(mMediaInfo));
        return contentValues;
    }

    @IntDef({STATE_NORMAL, STATE_CONNECTING})
    @Retention(RetentionPolicy.SOURCE)
    @interface State {
    }
}

@@ -103,16 +103,72 @@
 Cursor;%0A
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A
 import a
@@ -201,16 +201,16 @@
 IntDef;%0A
-
 import a
@@ -407,16 +407,38 @@
 ication 
+implements Parcelable 
 %7B%0A%0A    p
@@ -1870,23 +1870,1197 @@
 NFO)));%0A
-
     %7D%0A%0A
+    protected CastNotification(Parcel in) %7B%0A        mId = in.readInt();%0A        mTitle = in.readString();%0A        mContentText = in.readString();%0A        //noinspection ResourceType%0A        mState = in.readInt();%0A        mDeviceName = in.readString();%0A        mMediaInfo = CastNotificationManager.getInstance().getMediaInfoSerializer().toMediaInfo(in.readString());%0A    %7D%0A%0A    @Override%0A    public void writeToParcel(Parcel dest, int flags) %7B%0A        dest.writeInt(mId);%0A        dest.writeString(mTitle);%0A        dest.writeString(mContentText);%0A        dest.writeInt(mState);%0A        dest.writeString(mDeviceName);%0A        dest.writeString(CastNotificationManager.getInstance().getMediaInfoSerializer().toJson(mMediaInfo));%0A    %7D%0A%0A    @Override%0A    public int describeContents() %7B%0A        return 0;%0A    %7D%0A%0A    public static final Creator%3CCastNotification%3E CREATOR = new Creator%3CCastNotification%3E() %7B%0A        @Override%0A        public CastNotification createFromParcel(Parcel in) %7B%0A            return new CastNotification(in);%0A        %7D%0A%0A        @Override%0A        public CastNotification%5B%5D newArray(int size) %7B%0A            return new CastNotification%5Bsize%5D;%0A        %7D%0A    %7D;%0A%0A
     publ

make CastNotification Parcelable