using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.WebControls;
using System.Globalization;

namespace jeremythake.tvshowschedulesite {
  public class WebFeatureReceiver : SPFeatureReceiver {

    public override void FeatureActivated(SPFeatureReceiverProperties properties) {
        using (SPSite site = new SPSite("http://jt-asuslaptop/"))
        {
            using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                SPFile file = web.GetFile(web.Url + "/default.aspx");
                SPLimitedWebPartManager webpartsMng = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
                ContentByQueryWebPart contentByQueryWebPart = new ContentByQueryWebPart();
                contentByQueryWebPart.Title = "Schedule";
                contentByQueryWebPart.WebUrl = web.Url;
                
                SPListTemplateCollection listTemplates = web.Site.RootWeb.ListTemplates;
                SPListTemplate template = listTemplates["Schedule"];
                contentByQueryWebPart.BaseType = string.Empty;                
                contentByQueryWebPart.ServerTemplate = Convert.ToString((int)template.Type, CultureInfo.InvariantCulture);
                contentByQueryWebPart.CommonViewFields = "Air_x0020_Date,AirDate;Episode_x0020_Number,Episode;Season_x0020_Number,Season";
                contentByQueryWebPart.ItemStyle = "ScheduleStyle";
                //contentByQueryWebPart.ItemXslLink = site.Url + "/Style%20Library/jeremythake/schedule.xsl";   
                webpartsMng.AddWebPart(contentByQueryWebPart, "Right", 0);

                if (web.Lists["Schedule"].EventReceivers.Count == 0) //should be no other Event Receivers on this List
                {
                    web.Lists["Schedule"].EventReceivers.Add(
                      SPEventReceiverType.ItemAdded,
                      "jeremythake.tvshowschedulesite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8eb9a930004f2f1a",
                      "jeremythake.tvshowschedulesite.ScheduleListEventReceiver");
                }

                //setup navigation
                web.Navigation.UseShared = true;
                PublishingWeb pw = PublishingWeb.GetPublishingWeb(web);
                if (pw != null)
                {
                    pw.InheritGlobalNavigation = true;
                    pw.IncludePagesInNavigation = true;
                    pw.IncludeSubSitesInNavigation = false;
                    pw.NavigationShowSiblings = true;
                    pw.InheritCurrentNavigation = true;
                    pw.Update();
                    pw.Close();
                }
            }
        }
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
            SPFile file = web.GetFile(web.Url + "/default.aspx");
            SPLimitedWebPartManager webpartsMng = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
            for (int i=0; i< webpartsMng.WebParts.Count; i++)
                webpartsMng.DeleteWebPart(webpartsMng.WebParts[i]);

            for(int i=0; i<web.Lists["Schedule"].EventReceivers.Count; i++)
                web.Lists["Schedule"].EventReceivers[i].Delete();
        }
    }

    public override void FeatureInstalled(SPFeatureReceiverProperties properties) {
        /* no op */
    }
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {
        /* no op */
    }
  }
}
