using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Collections.Generic;


/// <summary>
/// Summary description for MSEvent
/// </summary>
public class MSEvent
{
    private int _id = 0;
    public int ID { 
        get { return _id;  } 
    }

    private DateTime _eventDate = DateTime.Now;
    public DateTime EventDate {
        set { _eventDate = value;  }
        get { return _eventDate;  } 
    }

    public string EventDateString {
        get { return _eventDate.ToShortDateString(); }
    }

    private string _title = "";
    public string Title {
        set { _title = value; }
        get { return _title;  } 
    }

    private string _description = "";
    public string Description {
        set { _description = value; }
        get { return _description;  } 
    }

	public MSEvent(int ID, DateTime EventDate, string Title, string Description )
	{
        _id = ID;
        _eventDate = EventDate;
        _title = Title;
        _description = Description;
	}

    public static MSEvent GetEventByID(int ID)
    {
        return SiteProvider.Events.GetEventByID(ID);
    }
    public static MSEventList CurrentEvents {
        get { return SiteProvider.Events.GetCurrentEvents();  }
    }
    public static MSEventList AllEvents {

        get { return SiteProvider.Events.GetAllEvents();  }
    }

    public void Save() 
    {
        string sql;
        if (_id == 0)
        {
            sql = "insert into Events (EventDate) values ('" + _eventDate + "');select @@identity";
            _id = Int32.Parse(SqlHelper.ExecuteScalar(CommandType.Text, sql).ToString());
        }
    
            SqlParameterList spl = new SqlParameterList();
            spl.AddParameter("Title", _title, SqlDbType.VarChar);
            spl.AddParameter("Description", _description, SqlDbType.Text);
            spl.AddParameter("EventDate", _eventDate, SqlDbType.DateTime);

            sql = "UPDATE Events SET " +
                " Title=@Title, " +
                " Description=@Description , " +
                " EventDate=@EventDate " +
                " WHERE id=" + _id;

            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, spl.ToArray());
        }
    }

