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

namespace TelerikStore
{
    public partial class CategoryView1 : System.Web.UI.Page
    {
        private static EBayData ebayContext;

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

        private static readonly String ViewFolder = "~/Views/";

        private static readonly String DefaultView = "ProductList_ListView.ascx";

        //Used to keep track of if we're on the RadGrid or RadListView page
        private string LoadedViewName
        {
            get
            {
                return (string)ViewState["LoadedView"] ?? DefaultView;
            }
            set
            {
                ViewState["LoadedView"] = value;
            }
        }

        private string CurrentCategory
        {
            get
            {
                return (string)ViewState["CurrentCategory"] ?? Request.QueryString["Category"];
            }
            set
            {
                ViewState["CurrentCategory"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Get an instance of our shopping cart labels and programmatically ajaxify them
            TwoColumn myMasterPage = (TwoColumn)this.Master;
            var itemsTotalLabel = myMasterPage.FindControl("ItemsTotalLabel");
            var priceTotalLabel = myMasterPage.FindControl("PriceTotalLabel");

            RadAjaxManager1.AjaxSettings.AddAjaxSetting(DisplayPlaceHolder, itemsTotalLabel, null);
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(DisplayPlaceHolder, priceTotalLabel, null);

            LoadView();
            ebayContext = new EBayData(new Uri(ebayString));
        }

        protected void ListViewButton_Click(object sender, EventArgs e)
        {
            LoadView("ProductList_ListView.ascx", CurrentCategory);
        }

        protected void GridButton_Click(object sender, EventArgs e)
        {
            LoadView("ProductList_Grid.ascx", CurrentCategory);
        }

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


        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            //Reset our Product list
            Session["Products"] = null;

            String[] arguments = e.Argument.Split(",".ToCharArray());

            //Simply loading the category
            if (arguments[0] == "Category")
            {
                //Load the new entries through our ajax call
                CurrentCategory = arguments[1];
                LoadView(LoadedViewName, arguments[1]);
            }
            
            //"Buy Now" was pressed in the Grid view.
            else
            {
                double productPrice = Convert.ToDouble(arguments[1]);
                BuyProduct(productPrice);
            }
        }

        private void BuyProduct(double productPrice)
        {
            CategoryView1 parentPage = (CategoryView1)this.Page;
            TwoColumn masterPage = (TwoColumn)parentPage.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 += productPrice;
            priceTotalLabel.Text = currentPriceTotal.ToString();

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

        private void LoadView()
        {
            LoadView(LoadedViewName, CurrentCategory);
        }

        //Load the particular View (RadGrid or RadListView) that we're interested in
        private void LoadView(string viewName, string categoryID)
        {
            if (viewName == "ProductList_ListView.ascx")
            {
                ProductList_ListView controlToAdd = LoadControl(string.Concat(ViewFolder, viewName)) as ProductList_ListView;

                controlToAdd.ID = viewName;
                controlToAdd.CategoryID = categoryID;
                LoadedViewName = viewName;
                DisplayPlaceHolder.Controls.Clear();
                DisplayPlaceHolder.Controls.Add(controlToAdd);
            }
            else if (viewName == "ProductList_Grid.ascx")
            {
                ProductList_Grid controlToAdd = LoadControl(string.Concat(ViewFolder, viewName)) as ProductList_Grid;

                controlToAdd.ID = viewName;
                controlToAdd.CategoryID = categoryID;
                LoadedViewName = viewName;
                DisplayPlaceHolder.Controls.Clear();
                DisplayPlaceHolder.Controls.Add(controlToAdd);
            }

        }
    }
}