using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Syndication;
using System.ServiceModel.Activation;
using System.Xml.Serialization;

[assembly: ContractNamespace("", ClrNamespace = "TwitterServiceLib")]

namespace TwitterServiceLib
{
    public class Status
    {
        public string Id;
        public DateTime Created;
        public string Username;
        public string Text;
    }

    [ServiceContract]
    public interface ITwitterService
    {
        [OperationContract]
        List<Status> GetStatuses();

        [OperationContract]
        List<Status> GetStatusByUser(string user);

        [OperationContract]
        Status GetStatus(string id);

        [OperationContract]
        void PostStatus(Status status);
    }

    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class TwitterService : ITwitterService
    {
        List<Status> statusdb = new List<Status>()
        {
            new Status() { Id = "1", Created = DateTime.Now, Username = "aaron", Text = "Having dinner with Fritz in China town." },
            new Status() { Id = "2", Created = DateTime.Now, Username = "aaron", Text = "Getting ready for my Web 2.0 + WCF talk" },
            new Status() { Id = "3", Created = DateTime.Now, Username = "fritz", Text = "Walking back from China Town" },
            new Status() { Id = "4", Created = DateTime.Now, Username = "fritz", Text = "Prepping demos for ASP.NET post-con workshop" },
            new Status() { Id = "5", Created = DateTime.Now, Username = "aaron", Text = "Looking at the X61 vs the equiv Dell" },
        };
        int id = 6;

        #region ITwitterService Members
            
        public List<Status> GetStatuses()
        {
            return this.statusdb;
        }

        public List<Status> GetStatusByUser(string name)
        {
            return this.statusdb.FindAll(
                s => s.Username.ToLower().Equals(name.ToLower()));
        }

        public Status GetStatus(string id)
        {
            return this.statusdb.Find(
                s => s.Id.Equals(id));
        }

        public void PostStatus(Status status)
        {
            status.Id = (id++).ToString();
            status.Created = DateTime.Now;
            this.statusdb.Add(status);
        }

        /*
        public SyndicationFeedFormatter GetStatusFeed(string format)
        {
            List<Status> statuses = this.GetStatuses();
            SyndicationFeed feed = new SyndicationFeed(
                "Pluralsight Twitter Feed", "A Twitter-like feed of status updates", null)
            {
                Items = from s in statuses
                        select new SyndicationItem()
                        {
                            Id = s.Id,
                            Title = new TextSyndicationContent(s.Text),
                            Content = new TextSyndicationContent(
                                string.Format("by {0} at {1}",
                                    s.Username, s.Created))
                        }
            };
            if (format.Equals("rss"))
                return new Rss20FeedFormatter(feed);
            else
                return new Atom10FeedFormatter(feed);
        }
        */

        #endregion
    }
}
