/*
 * Copyright (C) 2015 Mateusz Widuch. All rights reserved.
 */

package eu.redray.trevie;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Set;

/**
 * Represents movie item.
 */
public class Movie implements Parcelable {

    public static final String EXTRA_DETAILS = "movie_details";
    public static final Creator<Movie> CREATOR = new Creator<Movie>() {
        @Override
        public Movie createFromParcel(Parcel in) {
            return new Movie(in);
        }

        @Override
        public Movie[] newArray(int size) {
            return new Movie[size];
        }
    };

    private int mId;
    private String mTitle;
    private String mReleaseDate;
    private String mRating;
    private String mSynopsis;
    private String mPosterPath;
    private String mRuntime;
    private String mGenres;
    private String mCountries;
    private ArrayList mTrailerLinks;
    private ArrayList mUserReviews;

    public Movie(int id, String title, String releaseDate, String avgRating, String overview, String posterPath,
                 String runtime, String genres, String countries, ArrayList trailers, ArrayList reviews) {
        mId = id;
        mTitle = title;
        mReleaseDate = releaseDate;
        mRating = avgRating;
        mSynopsis = overview;
        mPosterPath = posterPath;
        mRuntime = runtime;
        mGenres = genres;
        mCountries = countries;
        mTrailerLinks = trailers;
        mUserReviews = reviews;
    }

    protected Movie(Parcel in) {
        mId = in.readInt();
        mTitle = in.readString();
        mReleaseDate = in.readString();
        mRating = in.readString();
        mSynopsis = in.readString();
        mPosterPath = in.readString();
        mRuntime = in.readString();
        mGenres = in.readString();
        mCountries = in.readString();
        mTrailerLinks = in.readArrayList(Uri.class.getClassLoader());
        mUserReviews = in.readArrayList(String.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mId);
        dest.writeString(mTitle);
        dest.writeString(mReleaseDate);
        dest.writeString(mRating);
        dest.writeString(mSynopsis);
        dest.writeString(mPosterPath);
        dest.writeString(mRuntime);
        dest.writeString(mGenres);
        dest.writeString(mCountries);
        dest.writeList(mTrailerLinks);
        dest.writeList(mUserReviews);
    }

    public int getId() {
        return mId;
    }

    public String getPosterPath() {
        return mPosterPath;
    }

    public String getTitle() {
        return mTitle;
    }

    public String getReleaseDate() {
        return mReleaseDate;
    }

    public String getRating() {
        return mRating;
    }

    public String getSynopsis() {
        return mSynopsis;
    }

    public String getReleaseYear() {
        return mReleaseDate.substring(0, 4);
    }

    public String getRuntime() {
        return mRuntime;
    }

    public void setTrailerLinks(ArrayList trailerLinks) {
        mTrailerLinks = trailerLinks;
    }

    public void setCountries(String countries) {
        mCountries = countries;
    }

    public void setGenres(String genres) {
        mGenres = genres;
    }

    public void setRuntime(String runtime) {
        mRuntime = runtime;
    }

    public String getGenres() {
        return mGenres;
    }

    public String getCountries() {
        return mCountries;
    }

    public ArrayList getTrailerLinks() {
        return mTrailerLinks;
    }

    public ArrayList getUserReviews() {
        return mUserReviews;
    }

    public void setUserReviews(ArrayList userReviews) {
        mUserReviews = userReviews;
    }

    /**
     * Checks if user has added movie to their favourites collection
     * @param favourites Set containing id's of favourtie movies
     * @return Returns true if movie is in favourites collection
     */
    public boolean isFavourite(Set<String> favourites) {
        return favourites.contains(String.valueOf(mId));
    }

    /**
     * Checks if additional data needs to be downloaded
     */
    public boolean needsMoreData() {
        return (mRuntime.equals("") && mCountries.equals("") && mGenres.equals("")
                && mTrailerLinks == null && mUserReviews == null);
    }
}

@@ -83,24 +83,135 @@
 ay.trevie;%0A%0A
+import android.content.Context;%0Aimport android.database.Cursor;%0Aimport android.database.sqlite.SQLiteDatabase;%0A
 import andro
@@ -330,16 +330,115 @@
 l.Set;%0A%0A
+import eu.redray.trevie.database.MoviesContract;%0Aimport eu.redray.trevie.database.MoviesDbHelper;%0A%0A
 /**%0A * R
@@ -4412,32 +4412,888 @@
 f(mId));%0A    %7D%0A%0A
+    /**%0A     * Checks if movie is present in favourites collection%0A     * @return true if it is, false otherwise%0A     */%0A    public boolean isFavourite(Context context) %7B%0A        MoviesDbHelper dbHelper = new MoviesDbHelper(context);%0A        SQLiteDatabase db = dbHelper.getReadableDatabase();%0A%0A        String selectString = %22SELECT * FROM %22 + MoviesContract.MoviesEntry.TABLE_NAME + %22 WHERE %22%0A                + MoviesContract.MoviesEntry._ID + %22 = ?%22;%0A%0A        // Put string in an array to avoid an unrecognized token error%0A        Cursor cursor = db.rawQuery(selectString, new String%5B%5D %7BString.valueOf(mId)%7D);%0A%0A        boolean isFavourite = false;%0A        if(cursor.moveToFirst())%7B%0A            isFavourite = true;%0A        %7D%0A%0A        // Close cursor%0A        cursor.close();%0A        // Close database%0A        db.close();%0A        return isFavourite;%0A    %7D%0A%0A
     /**%0A     * C

Change isFavourite method to check if movie is present in database