using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digioz.PictureModule;
using digioz;

public partial class controls_Pictures : System.Web.UI.UserControl
{
    public string lsImagePath = string.Empty;
    public string lsImageServerName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        string csURLEncryptionKey;

        if (!IsPostBack)
        {
            csURLEncryptionKey = ConfigurationManager.AppSettings["URLEncryptKey"];
            hypPictureUpload.NavigateUrl = "../Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Picture Upload", csURLEncryptionKey));

            categoriesDropDownList.DataSource = CategoryInfoManagement.GetCategoriesList("Picture");
            categoriesDropDownList.DataBind();
            categoriesDropDownList.Items.Insert(0, new ListItem("Select a Category"));
            categoriesDropDownList.Items.Insert(1, new ListItem("All Categories"));
            categoriesDropDownList.Items.Insert(2, new ListItem("Default Category"));

            if (Request.QueryString["category"] != null && Utility.IsNumeric(Request.QueryString["category"]))
            {
                int liSelectedCategory = Convert.ToInt32(Request.QueryString["category"]);
                string lsCategorySelected = CategoryInfoManagement.GetCategory(liSelectedCategory).CategoryName;
                categoriesDropDownList.SelectedValue = lsCategorySelected;
            }
            BindData();
        }
    }

	private void BindData()
	{
        string categoryName = String.Empty;

        if (categoriesDropDownList.SelectedIndex != -1 
            && categoriesDropDownList.SelectedIndex != 0
            && categoriesDropDownList.SelectedIndex != 1)
        {
            categoryName = categoriesDropDownList.SelectedItem.Text;
        }
        
        int liCurrentPage = 1;
		if (!(string.IsNullOrEmpty(LabelPageNum1.Text)))
		{
            liCurrentPage = Convert.ToInt32(LabelPageNum1.Text);
		}
		else
		{
			LabelPageNum1.Text = "1";
			LabelPageNum2.Text = "1";
		}
        PictureInfoList loPictureInfoList = PictureInfoManagement.GetPictureInfoList(categoryName);
		PagedDataSource loPS = new PagedDataSource();
        loPS.DataSource = loPictureInfoList;
		loPS.AllowPaging = true;
		loPS.PageSize = 9;
		loPS.CurrentPageIndex = liCurrentPage - 1;
		ButtonFirstPage1.Enabled = true;
		ButtonPrePage1.Enabled = true;
		ButtonNextPage1.Enabled = true;
		ButtonLastPage1.Enabled = true;
		ButtonFirstPage2.Enabled = true;
		ButtonPrePage2.Enabled = true;
		ButtonNextPage2.Enabled = true;
		ButtonLastPage2.Enabled = true;
		if (liCurrentPage == 1)
		{
			ButtonFirstPage1.Enabled = false;
			ButtonPrePage1.Enabled = false;
			ButtonFirstPage2.Enabled = false;
			ButtonPrePage2.Enabled = false;
		}
		if (liCurrentPage == loPS.PageCount)
		{
			ButtonNextPage1.Enabled = false;
			ButtonNextPage2.Enabled = false;
			ButtonLastPage1.Enabled = false;
			ButtonLastPage2.Enabled = false;
		}
		LabelPageAmount2.Text = Convert.ToString(loPS.PageCount);
		LabelPageAmount1.Text = Convert.ToString(loPS.PageCount);
		DataList1.DataSource = loPS;
		DataList1.DataBind();
	}
	protected void ButtonFirstPage1_Click(object sender, EventArgs e)
	{
        NavigateToFirstPage();
	}

	protected void ButtonFirstPage2_Click(object sender, EventArgs e)
	{
        NavigateToFirstPage();
	}
	protected void ButtonPrePage1_Click(object sender, EventArgs e)
	{
		LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) - 1);
		LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
	}
	protected void ButtonPrePage2_Click(object sender, EventArgs e)
	{
		LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) - 1);
		LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
	}
	protected void ButtonNextPage1_Click(object sender, EventArgs e)
	{
		LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) + 1);
		LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
	}
	protected void ButtonNextPage2_Click(object sender, EventArgs e)
	{
		LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) + 1);
		LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
	}
	protected void ButtonLastPage1_Click(object sender, EventArgs e)
	{
		LabelPageNum1.Text = LabelPageAmount1.Text;
		LabelPageNum2.Text = LabelPageAmount1.Text;
        BindData();
	}
	protected void ButtonLastPage2_Click(object sender, EventArgs e)
	{
		LabelPageNum1.Text = LabelPageAmount1.Text;
		LabelPageNum2.Text = LabelPageAmount1.Text;
        BindData();
	}
    public void ImageButton_Command(object sender, CommandEventArgs e)
    {
        int liPageID;

        if (e.CommandName == "ImagePath")
        {            
            lsImagePath = e.CommandArgument.ToString();

            if (lsImagePath.Contains("tn_"))
            {
                int liImgPathStart = lsImagePath.IndexOf("tn_");
                lsImageServerName = lsImagePath.Substring(liImgPathStart, lsImagePath.Length - liImgPathStart);
                lsImageServerName = lsImageServerName.Replace("tn_", "");                
            }            
            string lsFullPath = "../pictures/" + lsImageServerName;           
            Session["fullImage"] = lsFullPath;
           
            string lsPageName = "PictureViewer";
            if (lsPageName != null && lsPageName != String.Empty)
            {
                liPageID = MenuItemManagement.GetMenuItemFromMenuName(lsPageName).ID;
            }
            else
            {
                liPageID = 1;
            }

            int liPictureID = PictureInfoManagement.GetPictureIDFromName(lsImageServerName);
            int liCategoryID = -1;

            if (categoriesDropDownList.SelectedIndex > 2)
            {
                liCategoryID = (CategoryInfoManagement.GetCategoryBasedOnName(categoriesDropDownList.SelectedItem.ToString())).ID;
            }
           
            string lsRedirect = "Default.aspx?id=" + liPageID + "&content=" + liPictureID;

            if (liCategoryID > -1)
            {
                lsRedirect = lsRedirect + "&category=" + liCategoryID.ToString();
            }

            Response.Redirect(lsRedirect);
        }
    }

    protected void categoriesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        NavigateToFirstPage();
    }

    private void NavigateToFirstPage()
    {
        LabelPageNum1.Text = "1";
        LabelPageNum2.Text = "1";
        BindData();
    }
}
