package org.odk.collect.android.utilities;
/*
 * Copyright (c) 2015 Diona Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Diona
 * ("Confidential Information"). You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement you
 * entered into with Diona.
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;

import org.odk.collect.android.views.drawing.DrawingMode;
import org.odk.collect.android.views.drawing.DrawingTool;

/**
 * Preferences for the DrawView.
 */
public class DrawPreferences {

  private static final String PREF_COLOR = "color";
  private static final String PREF_WIDTH = "width";
  private static final String PREFS_DRAW = "draw";
  private static final int DEFAULT_TEXT_SIZE = 12;
  private static final String PREF_TEXT_SIZE = "textSize";
  private static final String PREF_DRAW_TOOL = "drawTool";
  private static final String PREF_DRAW_MODE = "drawMode";
  private static final String FONT_ITALICS = "fontItalics";
  private static final String FONT_BOLD = "fontBold";
  private static final String PREF_PAINT_STYLE = "paintStyle";

  private final SharedPreferences sharedPreferences;

  /**
   * @param context
   *     the current Context, used to access shared preferences.
   */
  public DrawPreferences(final Context context) {
    this.sharedPreferences = context.getSharedPreferences(PREFS_DRAW, Context.MODE_PRIVATE);
  }

  /**
   * @param widthPosition
   */
  public void setDrawWidthPosition(final int widthPosition) {
    this.sharedPreferences.edit().putInt(PREF_WIDTH, widthPosition).apply();
  }

  /**
   * @return
   */
  public int getDrawWidthPosition() {
    return sharedPreferences.getInt(PREF_WIDTH, 0);
  }

  /**
   * @param color
   *     the color to be used for the Paint in DrawView.
   */
  public void setDrawColor(final int color) {
    sharedPreferences.edit().putInt(PREF_COLOR, color).apply();
  }

  /**
   * @return the color to be used for the Paint in DrawView.
   */
  public int getDrawColor() {
    return sharedPreferences.getInt(PREF_COLOR, Color.BLACK);
  }

  /**
   * @param textSize
   *     the font size for text entered on a DrawView.
   */
  public void setTextSize(final int textSize) {
    sharedPreferences.edit().putInt(PREF_TEXT_SIZE, textSize).apply();
  }

  /**
   * @return the font size for text entered on a DrawView.
   */
  public int getTextSize() {
    return sharedPreferences.getInt(PREF_TEXT_SIZE, DEFAULT_TEXT_SIZE);
  }

  /**
   * @param paintStyle
   *     the last paint style used.
   */
  public void setPaintStyle(final Paint.Style paintStyle) {
    sharedPreferences.edit().putString(PREF_PAINT_STYLE, paintStyle.toString()).apply();
  }

  /**
   * @return the last paint style used.
   */
  public Paint.Style getPaintStyle() {
    return Paint.Style.valueOf(sharedPreferences.getString(PREF_PAINT_STYLE, Paint.Style.STROKE.toString()));
  }

  /**
   * @param drawingMode
   *     the last drawing mode used in DrawView.
   */
  public void setDrawMode(final DrawingMode drawingMode) {
    sharedPreferences.edit().putString(PREF_DRAW_MODE, drawingMode.toString()).apply();
  }

  /**
   * @return the last drawing mode used in DrawView.
   */
  public DrawingMode getDrawMode() {
    return DrawingMode.valueOf(sharedPreferences.getString(PREF_DRAW_MODE, DrawingMode.DRAW.toString()));
  }

  /**
   * @param drawingTool
   *     the last drawing tool used in DrawView.
   */
  public void setDrawTool(final DrawingTool drawingTool) {
    sharedPreferences.edit().putString(PREF_DRAW_TOOL, drawingTool.toString()).apply();
  }

  /**
   * @return the last drawing tool used in DrawView.
   */
  public DrawingTool getDrawTool() {
    return DrawingTool.valueOf(sharedPreferences.getString(PREF_DRAW_TOOL, DrawingTool.PEN.toString()));
  }

  /**
   * @param bold
   *     {@code true} if the text entered in DrawView should be bold, {@code false} if it should not.
   */
  public void setTextBold(final boolean bold) {
    sharedPreferences.edit().putBoolean(FONT_BOLD, bold).apply();
  }

  /**
   * @return {@code true} if the text entered in DrawView should be bold, {@code false} if it should not.
   */
  public boolean isTextBold() {
    return sharedPreferences.getBoolean(FONT_BOLD, false);
  }

  /**
   * @param italics
   *     {@code true} if the text entered in DrawView should be in italics, {@code false} if it should not.
   */
  public void setTextItalics(final boolean italics) {
    sharedPreferences.edit().putBoolean(FONT_ITALICS, italics).apply();
  }

  /**
   * @return {@code true} if the text entered in DrawView should be in italics, {@code false} if it should not.
   */
  public boolean isTextItalics() {
    return sharedPreferences.getBoolean(FONT_ITALICS, false);
  }
}
