using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;

using System.Linq;

namespace HolidayManager.Db
{
    public class HolidayContext : DataContext
    {
        public static string ConnectionString = "Data Source=isostore:/HolidayManager.sdf";

        public HolidayContext(string connectionString) : base(connectionString) 
        {
        }

        public Table<LocalHoliday> LocalHolidays;
        public Table<MyHoliday> MyHolidays;

        public void InsertMyHoliday(DateTime selectedDate, HolidayType type)
        {
            MyHoliday mh = new MyHoliday();
            mh.Date = selectedDate;
            mh.Type = type;

            // 중복 체크
            RemoveMyHoliday(false, selectedDate);

            this.MyHolidays.InsertOnSubmit(mh);
            this.SubmitChanges();
        }

        internal void RemoveMyHoliday(bool commitChanges, DateTime dateToRemove)
        {
            var dates = (from entity in this.MyHolidays 
             where entity.Date == dateToRemove
             select entity ).ToList();

            this.MyHolidays.DeleteAllOnSubmit(dates);

            if (commitChanges == true)
            {
                this.SubmitChanges();
            }
        }

        internal void ToggleHoliday(DateTime dateTime)
        {
            var dates = (from entity in this.MyHolidays
                         where entity.Date == dateTime
                         select entity).ToList();

            if (dates.Count() >= 1)
            {
                this.MyHolidays.DeleteAllOnSubmit(dates);
            }
            else
            {
                InsertMyHoliday(dateTime, HolidayType.Full);
            }

            this.SubmitChanges();
        }

        //public void InsertLocalHoliday(DateTime dt)
        //{
        //    LocalHoliday lh = new LocalHoliday();
        //    lh.Date = dt;
        //    this.LocalHolidays.InsertOnSubmit(lh);
        //    this.SubmitChanges();
        //}
    }

    [Table]
    public class LocalHoliday
    {
        DateTime date;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "DATETIME NOT NULL", CanBeNull = false)]
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }
    }

    public enum HolidayType
    {
        None,
        Full,
        Half,
    }

    [Table]
    public class MyHoliday
    {
        public MyHoliday()
        {
        }

        public MyHoliday(DateTime date, HolidayType type)
        {
            this.Date = date;
            this.Type = type;
        }

        DateTime date;
        [Column(IsPrimaryKey = true, DbType = "DATETIME NOT NULL", CanBeNull = false)]
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }

        HolidayType type;
        [Column(DbType = "int NOT NULL", CanBeNull = false)]
        public HolidayType Type
        {
            get { return type; }
            set { type = value; }
        }
    }
}
