using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml.Serialization;
using Wpf_Demo.DataAccess;
using Wpf_Demo.ViewModels;

namespace Wpf_Demo.ViewModels
{
    public class GoalViewModel : BusinessBase<GoalViewModel, long>, IComparable<GoalViewModel>
    {

        private string goal = string.Empty;
        public string Goal
        {
            get
            {
                return goal;
            }
            set
            {
                if (value != goal)
                {
                    goal = value;
                    MarkChanged("Goal");
                }
            }
        }

        [XmlElement("Priority")]
        public int PriorityInt
        {
            get { return (int)Priority; }
            set { Priority = (GoalPriority)value; }
        }

       
        private GoalPriority priority = GoalPriority.Medium;
        
        [XmlIgnore]
        public GoalPriority Priority
        {
            get
            {
                return priority;
            }
            set
            {
                if (value != priority)
                {
                    priority = value;
                    MarkChanged("Priority");
                }
            }
        }

        private decimal cost = 0;
        public decimal Cost
        {
            get
            {
                return cost;
            }
            set
            {
                if (value != cost)
                {
                    cost = value;
                    MarkChanged("Cost");
                }
            }
        }

        private bool done = false;
        public bool Done
        {
            get { return done; }
            set
            {
                if (value != done)
                {
                    done = value;
                    MarkChanged("Done");
                }
            }
        }

        public GoalViewModel Clone()
        {
            return (GoalViewModel)this.MemberwiseClone();
        }


        #region Overriden Methods
        public override string ToString()
        {
            return Goal;
        }
        
        public override bool Equals(object obj)
        {
            if (obj is GoalViewModel)
                return (obj as GoalViewModel).Id.Equals(this.Id);
            else
                return false;
        }

        public bool Equals(GoalViewModel obj)
        {
            return Id.Equals(obj);
        }

        #endregion

        #region IComparable<ItemViewModel> Members

        public int CompareTo(GoalViewModel other)
        {
            return Priority.CompareTo(other.Priority);
        }

        #endregion

        protected override void ValidateRules()
        {
            AddRule("Goal","Goal is required.", string.IsNullOrEmpty(Goal));
        }

        protected override GoalViewModel DataSelect(long id)
        {
            return DatabaseAccessLayer.Items.FirstOrDefault(i => i.Id.Equals(id));
        }

        protected override void DataUpdate()
        {
            DatabaseAccessLayer.UpdateItem(this);
        }

        protected override void DataInsert()
        {
            DatabaseAccessLayer.AddItem(this);
        }

        protected override void DataDelete()
        {
            DatabaseAccessLayer.DeleteItem(this);
        }

        #region Static Methods
        public static void Save(IEnumerable<GoalViewModel> items)
        {
            DatabaseAccessLayer.AddItems(items);
        }

        public static void Delete(IEnumerable<GoalViewModel> items)
        {
            DatabaseAccessLayer.DeleteItems(items);
        }

        public static void Update(IEnumerable<GoalViewModel> items)
        {
            DatabaseAccessLayer.UpdateItems(items);
        }

        #endregion
    }
}