using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Services.Client;
using TelerikStore.EbayService;
using Telerik.Web.UI;
using System.Web.Services;


namespace TelerikStore
{
    public partial class Default : System.Web.UI.Page
    {
        private static DataServiceContext context;

        private static EBayData ebayContext;

        private static readonly String ebayString = "http://ebayodata.cloudapp.net";

        private static readonly int maximumCategories = 14;

        private static readonly int maximumPages = 3;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Get an instance of our eBay OData feed context and get the top three deals
                ebayContext = new EBayData(new Uri(ebayString));

                String dealString = "Deals?$top=3";

                var items = ebayContext.Execute<Deal>(new Uri(dealString, UriKind.Relative)).ToList();

                TempListView.DataSource = items;
            }
        }

        protected void SearchButtonClick(object sender, EventArgs e)
        {
            Response.Redirect("ProductView.aspx?Search=" + SearchBox.Text);
        }

        protected void Category_Click(object sender, EventArgs e)
        {
            LinkButton triggerButton = (LinkButton)sender;
            string triggerText = triggerButton.CommandArgument;

            Response.Redirect("CategoryView.aspx?Category=" + triggerText);
        }

        //Simply Generate a list of CategoryItems to be displayed
        [WebMethod]
        public static List<CategoryItem> GenerateCategories(int startIndex)
        {
            List<CategoryItem> CategoryList = new List<CategoryItem>();

            CategoryList.Add(new CategoryItem { CategoryName = "Antiques", CategoryId = "20081" });
            CategoryList.Add(new CategoryItem { CategoryName = "Art", CategoryId = "550" });
            CategoryList.Add(new CategoryItem { CategoryName = "Baby", CategoryId = "2984" });
            CategoryList.Add(new CategoryItem { CategoryName = "Books", CategoryId = "267" });
            CategoryList.Add(new CategoryItem { CategoryName = "Business & Industrial", CategoryId = "12576" });
            CategoryList.Add(new CategoryItem { CategoryName = "Cameras & Photo", CategoryId = "625" });
            CategoryList.Add(new CategoryItem { CategoryName = "Cell Phones & PDAs", CategoryId = "15032" });
            CategoryList.Add(new CategoryItem { CategoryName = "Clothing, Shoes & Accessories", CategoryId = "11450" });
            CategoryList.Add(new CategoryItem { CategoryName = "Coins & Paper Money", CategoryId = "11116" });
            CategoryList.Add(new CategoryItem { CategoryName = "Collectibles", CategoryId = "1" });
            CategoryList.Add(new CategoryItem { CategoryName = "Computers & Networking", CategoryId = "58058" });
            CategoryList.Add(new CategoryItem { CategoryName = "Consumer Electronics", CategoryId = "293" });
            CategoryList.Add(new CategoryItem { CategoryName = "Crafts", CategoryId = "14339" });
            CategoryList.Add(new CategoryItem { CategoryName = "Dolls & Bears", CategoryId = "237" });
            CategoryList.Add(new CategoryItem { CategoryName = "DVDs & Movies", CategoryId = "11232" });
            CategoryList.Add(new CategoryItem { CategoryName = "Entertainment Memorabilia", CategoryId = "45100" });
            CategoryList.Add(new CategoryItem { CategoryName = "Gift Cards & Coupons", CategoryId = "172008" });
            CategoryList.Add(new CategoryItem { CategoryName = "Health & Beauty", CategoryId = "26395" });
            CategoryList.Add(new CategoryItem { CategoryName = "Home & Garden", CategoryId = "11700" });
            CategoryList.Add(new CategoryItem { CategoryName = "Jewelry & Watches", CategoryId = "281" });
            CategoryList.Add(new CategoryItem { CategoryName = "Music", CategoryId = "11233" });
            CategoryList.Add(new CategoryItem { CategoryName = "Musical Instruments", CategoryId = "619" });
            CategoryList.Add(new CategoryItem { CategoryName = "Pet Supplies", CategoryId = "1281" });
            CategoryList.Add(new CategoryItem { CategoryName = "Pottery & Glass", CategoryId = "870" });
            CategoryList.Add(new CategoryItem { CategoryName = "Real Estate", CategoryId = "10542" });
            CategoryList.Add(new CategoryItem { CategoryName = "Specialty Services", CategoryId = "316" });
            CategoryList.Add(new CategoryItem { CategoryName = "Sporting Goods", CategoryId = "382" });
            CategoryList.Add(new CategoryItem { CategoryName = "Sports Mem, Cards & Fan Shop", CategoryId = "64482" });
            CategoryList.Add(new CategoryItem { CategoryName = "Stamps", CategoryId = "260" });
            CategoryList.Add(new CategoryItem { CategoryName = "Tickets", CategoryId = "1305" });
            CategoryList.Add(new CategoryItem { CategoryName = "Toys & Hobbies", CategoryId = "220" });
            CategoryList.Add(new CategoryItem { CategoryName = "Travel", CategoryId = "3252" });
            CategoryList.Add(new CategoryItem { CategoryName = "Video Games", CategoryId = "1249" });

            //Just checking if we are out of range in the category list or not
            if ((startIndex + maximumCategories) > CategoryList.Count)
            {
                int endIndex = CategoryList.Count - startIndex;
                return CategoryList.GetRange(startIndex, endIndex);
            }
            else
            {
                return CategoryList.GetRange(startIndex, maximumCategories);
            }
        }

        //Simple POCO class to use in our PageMethod
        public class CategoryItem
        {
            public String CategoryName { get; set; }

            public String CategoryId { get; set; }

            public CategoryItem()
            {
            }
        }
    }
}