package @APP_PACKAGE@;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;

import @APP_PACKAGE@.R;

/*
 * This class builds recommendations as notifications with videos as inputs.
 */
public class XBMCRecommendationBuilder
{
  private static final String TAG = "@APP_NAME@";

  private Context mContext;

  private int mId;
  private int mPriority;
  private int mSmallIcon;
  private String mTitle;
  private String mDescription;
  private Bitmap mBitmap;
  private String mBackgroundUri;
  private PendingIntent mIntent;

  // Constants from API >= 21
  final String API_NOTIFICATION_EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
  final String API_NOTIFICATION_CATEGORY_RECOMMENDATION = "recommendation";

  public XBMCRecommendationBuilder()
  {
  }

  public XBMCRecommendationBuilder setContext(Context context)
  {
    mContext = context;
    return this;
  }

  public XBMCRecommendationBuilder setId(int id)
  {
    mId = id;
    return this;
  }

  public XBMCRecommendationBuilder setPriority(int priority)
  {
    mPriority = priority;
    return this;
  }

  public XBMCRecommendationBuilder setTitle(String title)
  {
    mTitle = title;
    return this;
  }

  public XBMCRecommendationBuilder setDescription(String description)
  {
    mDescription = description;
    return this;
  }

  public XBMCRecommendationBuilder setBitmap(Bitmap bitmap)
  {
    mBitmap = bitmap;
    return this;
  }

  public XBMCRecommendationBuilder setBackground(String uri)
  {
    mBackgroundUri = uri;
    return this;
  }

  public XBMCRecommendationBuilder setIntent(PendingIntent intent)
  {
    mIntent = intent;
    return this;
  }

  public XBMCRecommendationBuilder setSmallIcon(int resourceId)
  {
    mSmallIcon = resourceId;
    return this;
  }

  public Notification build()
  {

    Log.d(TAG, "Building notification - " + this.toString());

    Bundle extras = new Bundle();
    if (mBackgroundUri != null)
    {
      Log.d(TAG, "Background - " + mBackgroundUri);
      extras.putString(API_NOTIFICATION_EXTRA_BACKGROUND_IMAGE_URI, mBackgroundUri);
    }

    Notification notification = new Notification.BigPictureStyle(
            new Notification.Builder(mContext)
                    .setContentTitle(mTitle)
                    .setContentText(mDescription)
                    .setPriority(mPriority)
                    .setLocalOnly(true)
                    .setOngoing(true)
                    .setColor(mContext.getResources().getColor(R.color.recommendation_color))
                    .setCategory(API_NOTIFICATION_CATEGORY_RECOMMENDATION)
                    .setLargeIcon(mBitmap)
                    .setSmallIcon(mSmallIcon)
                    .setContentIntent(mIntent)
                    .setExtras(extras)
                    .setAutoCancel(false)
    ).build();

    return notification;
  }

  @Override
  public String toString()
  {
    return "RecommendationBuilder{" + ", mId=" + mId + ", mPriority="
            + mPriority + ", mSmallIcon=" + mSmallIcon + ", mTitle='" + mTitle
            + '\'' + ", mDescription='" + mDescription + '\'' + ", mBitmap='"
            + mBitmap + '\'' + ", mBackgroundUri='" + mBackgroundUri + '\''
            + ", mIntent=" + mIntent + '}';
  }
}
