using System;
using System.Drawing;
using System.IO;
using digioz;
using digioz.PictureModule;

public partial class controls_pictureViewerl : System.Web.UI.UserControl
{
    public string csImgName;
    public PictureInfo coPictureInfo;
    public string csImgPath = String.Empty;
    public string csUsername = string.Empty;
    public PortalSettings coPortalSettings;
    public string csCategoryName;
    public int ciCategoryID;

    public enum PictureDirection
    {
        Forward,
        Back
    }

    private void SetUserHyperLink(User poUser)
    {
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();
        hypUsername.HRef = "../Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Profile", coPortalSettings.URLEncryptionKey))
                                 + "&username=" + Server.UrlEncode(digioz.Encryption64.Encrypt(poUser.Username, coPortalSettings.URLEncryptionKey));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        int liPictureID = -1;
        User loUser = new User();
        string lsPhysicalImgPath = String.Empty;
        string lsUrlReferrer = Request.Url.ToString();

        try
        {
            liPictureID = Convert.ToInt32(Request.QueryString["content"]);
            coPictureInfo = PictureInfoManagement.GetPictureInfo(liPictureID);
            loUser = UserManagement.GetUserByID(Convert.ToInt32(coPictureInfo.Uploader));
            SetUserHyperLink(loUser);
            csUsername = loUser.Username;

            csImgPath = lsUrlReferrer.Substring(0, lsUrlReferrer.LastIndexOf("/")) + "/" + "pictures" + "/" + coPictureInfo.Serverfilename;
            lsPhysicalImgPath = GetPhysicalPicPath(coPictureInfo.Serverfilename);
            ScaleImageControl(lsPhysicalImgPath);
            Image1.ImageUrl = csImgPath;
        }
        catch { }

        try
        {
            ciCategoryID = Convert.ToInt16(Request.QueryString["category"]);
            csCategoryName = CategoryInfoManagement.GetCategory(ciCategoryID).CategoryName;
        }
        catch { }

        if (Session["username"] == null)
        {
            btnFavorites.Enabled = false;
        }

        Image1Link.HRef = csImgPath;
    }


    protected void btnDownload_Click(object sender, EventArgs e)
    {
        string imagePath = string.Empty;
        string imageName = string.Empty;
        string securePath = string.Empty;
        FileStream sourceFile;
        byte[] getContent;
        long FileSize;

        imagePath = csImgPath;
        imageName = Path.GetFileName(imagePath);
        imagePath = GetPhysicalPicPath(imageName);
        securePath = imagePath;
        string ext = Path.GetExtension(imageName);
        ext = ext.Substring(1, ext.Length - 1);

        digioz.ExtensionsList loExtensionsList = digioz.ExtensionsManagement.GetExtensionsList();

        foreach (digioz.Extensions loExtension in loExtensionsList)
        {
            if (ext == loExtension.Name)
            {
                Response.ContentType = loExtension.MimeType;
                Response.AddHeader("content-disposition", "attachment; filename=" + imageName);
                sourceFile = new FileStream(securePath, FileMode.Open, FileAccess.Read);
                FileSize = sourceFile.Length;
                getContent = new byte[(int)FileSize];
                sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                sourceFile.Close();
                Response.BinaryWrite(getContent);
                break;
            }
        }
    }

    /// <summary>
    /// btnFavorites_Click
    /// </summary>
    /// <param name="object sender"></param>
    /// <param name="EventArgs e"></param>
    protected void btnFavorites_Click(object sender, EventArgs e)
    {
        string lsTableId = string.Empty;
        string lsImagePath = string.Empty;
        string lsType = string.Empty;
        string lsImageName = string.Empty;
        string lsImageUrl = string.Empty;
        string lsUrlReferrer = Request.Url.ToString();

        lsType = "picture";
        lsImagePath = csImgPath;
        lsImageName = Path.GetFileName(lsImagePath);

        PictureInfoManagement loPictureInfoManagement = new PictureInfoManagement();
        lsTableId = Convert.ToString(PictureInfoManagement.GetPictureIDFromName(lsImageName));
        loPictureInfoManagement.SaveToFavorites(lsType, lsTableId, lsImageName);
    }

    protected void btnPrev_Click(object sender, EventArgs e)
    {
        FlipPicture(PictureDirection.Back);
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        FlipPicture(PictureDirection.Forward);
    }

    public string GetPicture(string psFileName)
    {
        string lsFullPath = "../pictures/" + psFileName;
        return lsFullPath;
    }

    public string GetPhysicalPicPath(string psFileName)
    {
        string physicalPath = Request.PhysicalPath.ToLower();
        physicalPath = physicalPath.Replace("default.aspx", "pictures");
        string lsFullPath = physicalPath + "\\" + psFileName;
        return lsFullPath;
    }

    public void FlipPicture(PictureDirection loPictureDirection)
    {
        string lsDirection = String.Empty;
        string lsRedirect = String.Empty;
        int liNewPictureID = 0;

        if (loPictureDirection == PictureDirection.Forward)
        {
            lsDirection = "B";
        }
        else
        {
            lsDirection = "F";
        }

        liNewPictureID = PictureInfoManagement.FlipPicture(coPictureInfo.ID, lsDirection, csCategoryName);

        lsRedirect = "Default.aspx?id=" + Request.QueryString["id"] + "&content=" + liNewPictureID;

        if (csCategoryName != null && csCategoryName.Length > 0)
        {
            lsRedirect = lsRedirect + "&category=" + ciCategoryID.ToString();
        }

        Response.Redirect(lsRedirect);
    }

    public void ScaleImageControl(string imgPath)
    {
        Bitmap bmpImage = new Bitmap(imgPath);

        if (bmpImage.Height > bmpImage.Width)
        {
            float scale = 375.0f / System.Math.Max(bmpImage.Height, bmpImage.Width);
            System.Drawing.Image bmpScale = bmpImage.GetThumbnailImage(
                (int)(bmpImage.Width * scale), (int)(bmpImage.Height * scale),
                null, System.IntPtr.Zero);

            Image1.Width = bmpScale.Width;
            Image1.Height = bmpScale.Height;
        }
        else
        {
            float scale = 580.0f / System.Math.Max(bmpImage.Height, bmpImage.Width);
            System.Drawing.Image bmpScale = bmpImage.GetThumbnailImage(
                (int)(bmpImage.Width * scale), (int)(bmpImage.Height * scale),
                null, System.IntPtr.Zero);

            Image1.Width = bmpScale.Width;
            Image1.Height = bmpScale.Height;
        }
    }
}
