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 TelerikStore.MasterPages;

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

        private static EBayData ebayContext;

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

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                ebayContext = new EBayData(new Uri(ebayString));

                //Get the items we searched for
                List<Item> searchResults = GetSearchedItems();

                if (searchResults.Count > 0)
                {
                    PopulateProductElements(searchResults[0]);
                    ProductNextButton.Enabled = true;
                    Session["SearchResults"] = searchResults;
                }

                //Keep track of the current page
                ViewState["ItemsIndex"] = 0;

            }
        }
        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            TwoColumn masterPage = (TwoColumn)this.Master;
            Label priceTotalLabel = (Label)masterPage.FindControl("PriceTotalLabel");
            Label itemsTotalLabel = (Label)masterPage.FindControl("ItemsTotalLabel");

            RadAjaxManager1.AjaxSettings.AddAjaxSetting(priceTotalLabel, null);
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(itemsTotalLabel, null);
       }
        protected void BuyButton_Click(object sender, EventArgs e)
        {
            TwoColumn masterPage = (TwoColumn)this.Master;

            Label priceTotalLabel = (Label)masterPage.FindControl("PriceTotalLabel");
            Label itemsTotalLabel = (Label)masterPage.FindControl("ItemsTotalLabel");

            int currentTotalItems = Convert.ToInt16(itemsTotalLabel.Text);
            currentTotalItems++;
            itemsTotalLabel.Text = currentTotalItems.ToString();

            double currentPriceTotal = Convert.ToDouble(priceTotalLabel.Text);
            currentPriceTotal += Convert.ToDouble(ProductPriceLabel.Text);
            priceTotalLabel.Text = currentPriceTotal.ToString();

            HttpContext.Current.Cache["PriceTotal"] = currentPriceTotal;
            HttpContext.Current.Cache["ItemsTotal"] = currentTotalItems;
        }

        protected void QuickCategoryMenu_ItemClick(object sender, RadMenuEventArgs e)
        {
            Response.Redirect("CategoryView.aspx?Category=" + e.Item.Value);
        }

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

        protected void ProductPreviousButton_Click(object sender, EventArgs e)
        {
            //Get the items we searched for and the current index
            List<Item> searchedItems = Session["SearchResults"] as List<Item>;
            int itemIndex = (int)ViewState["ItemsIndex"];

            itemIndex--;

            PopulateProductElements(searchedItems[itemIndex]);

            ViewState["ItemsIndex"] = itemIndex;

            if (itemIndex == 0)
            {
                ProductPreviousButton.Enabled = false;
            }
        }

        protected void ProductNextButton_Click(object sender, EventArgs e)
        {
            //Get the items we searched for and the current index
            List<Item> searchedItems = Session["SearchResults"] as List<Item>;
            int itemIndex = (int)ViewState["ItemsIndex"];

            itemIndex++;

            PopulateProductElements(searchedItems[itemIndex]);

            ViewState["ItemsIndex"] = itemIndex;

            if (ProductPreviousButton.Enabled == false)
            {
                ProductPreviousButton.Enabled = true;
            }

            if (itemIndex == searchedItems.Count)
            {
                ProductNextButton.Enabled = false;
            }
        }

        //Populate all of our product-related controls
        private void PopulateProductElements(Item itemToPopulateWith)
        {
            ProductImage.ImageUrl = itemToPopulateWith.GalleryUrl;
            ProductTitleLabel.Text = itemToPopulateWith.Title;
            ProductDescriptionLabel.Text = itemToPopulateWith.Description ?? "No Description";
            ProductPriceLabel.Text = String.Format((itemToPopulateWith.CurrentPrice).ToString());
        }

        private List<Item> GetSearchedItems()
        {
            List<Item> listToReturn = new List<Item>();

            string searchItemToFind = Request.QueryString["Search"];

            if (searchItemToFind != null)
            {
                String itemSearchString = String.Format("Items?search={0}", searchItemToFind);

                listToReturn = ebayContext.Execute<Item>(new Uri(itemSearchString, UriKind.Relative)).ToList();
            }

            return listToReturn;
        }
    }
}