/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 * +================================================================================+
 *
 *    Authors:
 *    David Krummenacher (dave@timbookedtwo.com)
 *
 */

using System;
using UnityEngine;

namespace Utils.Settings {

	/// <summary>
	/// Camera settings.
	/// </summary>
	public class CameraSetting : Setting {

		/// <summary>
		/// Determines whether the x axis should be inverted.
		/// </summary>
		private bool invertXAxis;
		/// <summary>
		/// Determines whether the x axis should be inverted.
		/// </summary>
		public bool InvertXAxis {
			get { return invertXAxis; }
			set {
				invertXAxis = value;
				RaiseOnValueChanged(this);
			}
		}

		/// <summary>
		/// Determines whether the y axis should be inverted.
		/// </summary>
		private bool invertYAxis;
		/// <summary>
		/// Determines whether the y axis should be inverted.
		/// </summary>
		public bool InvertYAxis {
			get { return invertYAxis; }
			set {
				invertYAxis = value;
				RaiseOnValueChanged(this);
			}
		}

		/// <summary>
		/// The camera sensitivity.
		/// </summary>
		private int cameraSensitivity;
		/// <summary>
		/// The camera sensitivity.
		/// </summary>
		public int CameraSensitivity {
			get { return cameraSensitivity; }
			set {
				cameraSensitivity = value;
				RaiseOnValueChanged(this);
			}
		}

		/// <summary>
		/// The key for invert x axis used in the player preferences.
		/// </summary>
		public static string InvertXAxisKey = "InvertXAxis";
		/// <summary>
		/// The key for invert y axis used in the player preferences.
		/// </summary>
		public static string InvertYAxisKey = "InvertYAxis";
		/// <summary>
		/// The key for the camera sensitivity used in the player preferences.
		/// </summary>
		public static string CameraSensitivityKey = "CameraSensitivity";

		/// <summary>
		/// Initializes the camera settings.
		/// </summary>
		protected override void InitSetting() {
			LoadSetting();
		}

		/// <summary>
		/// Loads the quality settings from the player preferences.
		/// </summary>
		protected override void LoadSetting() {
			if (HasKey(InvertXAxisKey)) invertXAxis = GetBool(InvertXAxisKey);
			else invertXAxis = false;

			if (HasKey(InvertYAxisKey)) invertYAxis = GetBool(InvertYAxisKey);
			else invertYAxis = true;

			if (HasKey(CameraSensitivityKey)) cameraSensitivity = Mathf.Clamp(GetInt(CameraSensitivityKey), 1, 10);
			else cameraSensitivity = 5;

			// Raise on value changed event
			RaiseOnValueChanged(this);
		}

		/// <summary>
		/// Saves the quality settings to the player preferences.
		/// </summary>
		protected override void SaveSetting() {
			SetBool(InvertXAxisKey, InvertXAxis);
			SetBool(InvertYAxisKey, InvertYAxis);
			SetInt(CameraSensitivityKey, CameraSensitivity);
		}
	}
}
