﻿using System;

namespace Aurora.Database
{
    public static class Cache
    {
        public static Models.Layout Layout;
        public static Models.Overview Overview;

        /// <summary>Stores a cached model using the specified model data.</summary>
        /// <typeparam name="T">The type of the model to store.</typeparam>
        /// <param name="model">The model to store in the cache.</param>
        /// <returns>Returns whether the operation was successful.</returns>
        public static bool Store<T>(T model)
        {
            Type type = typeof(T);
            switch (type.Name)
            {
                case "Layout":
                    try
                    {
                        if (Layout != null)
                            throw new Exception("Layout model cannot be stored because a model has already been stored. Please update model after storing it.");
                        Layout = model as Models.Layout;
                        return true;
                    }
                    catch (Exception) { return false; }

                case "Overview":
                    try
                    {
                        if (Overview != null)
                            throw new Exception("Overview model cannot be stored because a model has already been stored. Please update model after storing it.");
                        Overview = model as Models.Overview;
                        return true;
                    }
                    catch (Exception) { return false; }

                default:
                    return false;
            }
        }

        /// <summary>Updates the cached model of the specified type with the data from the specified uncached model.</summary>
        /// <typeparam name="T">The type of the model to update.</typeparam>
        /// <param name="model">The model to update the cached model with.</param>
        /// <returns>Returns whether the operation was successful.</returns>
        public static bool Update<T>(T model)
        {
            Type type = typeof(T);
            switch (type.Name)
            { 
                case "Layout":
                    try
                    {
                        if (Layout == null)
                            throw new Exception("Layout model cannot be updated because no model has been stored. Please store a model before updating it.");
                        Layout = model as Models.Layout;
                        return true;
                    }
                    catch(Exception) { return false; }

                case "Overview":
                    try
                    {
                        if (Overview == null)
                            throw new Exception("Overview model cannot be updated because no model has been stored. Please store a model before updating it.");
                        Overview = model as Models.Overview;
                        return true;
                    }
                    catch (Exception) { return false; }

                default:
                    return false;
            }
        }

        /// <summary>Retrieves the model of the designated type from the cache.</summary>
        /// <typeparam name="T">The type of the model to retrieve.</typeparam>
        /// <param name="model">The model to assign the cached model to.</param>
        /// <returns>Returns whether the operation was successful.</returns>
        public static bool Retrieve<T>(ref T model)
        {
            Type type = typeof(T);
            switch(type.Name)
            {
                case "Layout":
                    try
                    {
                        if (Layout == null)
                            throw new Exception("Layout model cannot be retrieved because no model has been stored. Please store a model before retrieving it.");
                        model = (T)Convert.ChangeType(Layout, type);
                        return true;
                    }
                    catch (Exception) { return false; }
                case "Overview":
                    try
                    {
                        if (Overview == null)
                            throw new Exception("Layout model cannot be retrieved because no model has been stored. Please store a model before retrieving it.");
                        model = (T)Convert.ChangeType(Overview, type);
                        return true;
                    }
                    catch (Exception) { return false; }
                default:
                    return false;
            }
        }
    }
}