using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using digioz;
using digioz.PictureModule;

public partial class controls_ProfileAvatarUpload : System.Web.UI.UserControl
{
    private bool _fileIsOk = false;
    private string[] _extension = { ".jpg", ".gif", ".bmp", ".png" };
    PortalSettings coPortalSettings;

    protected void Page_Load(object sender, EventArgs e)
    {
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        foreach (string _ext in _extension)
        {
            if (uplAvatar.PostedFile.FileName.ToLower().EndsWith(_ext))
            {
                _fileIsOk = true;
            }
        }
        if (!(_fileIsOk))
        {
            lblMessage.Text = "This file format is not accepted for upload !";
            return;
        }
                
        string ext = uplAvatar.PostedFile.FileName.ToLower().Substring(uplAvatar.PostedFile.FileName.ToLower().LastIndexOf("."));
        string filename = "";
        string serverPath = Server.MapPath("~") + "\\pictures\\avatar\\" + filename + ext;
        uplAvatar.PostedFile.SaveAs(serverPath);

        System.Drawing.Image mymap = System.Drawing.Image.FromFile(serverPath);
        int width = 0, height = 0;
        if (mymap.Width > mymap.Height)
        {
            width = 85;
            height = Convert.ToInt32(((float)mymap.Height / (float)mymap.Width) * 85);
        }
        else
        {
            height = 85;
            width = Convert.ToInt32(((float)mymap.Width / (float)mymap.Height) * 85);
        }
        System.Drawing.Image thumbnail = mymap.GetThumbnailImage(width, height, null, IntPtr.Zero);
        ImageFormat extFormat = ImageFormat.Jpeg;
        switch (ext)
        {
            case ".jpg":
                extFormat = ImageFormat.Jpeg;
                break;
            case ".gif":
                extFormat = ImageFormat.Gif;
                break;
            case ".png":
                extFormat = ImageFormat.Png;
                break;
            case ".bmp":
                extFormat = ImageFormat.Bmp;
                break;
        }

        string csUser; 
        csUser = Convert.ToString(Session["username"]);

        thumbnail.Save(Server.MapPath("~") + "/pictures/avatar/" + csUser+ 
            "_avatar" + ext, extFormat);
        thumbnail.Dispose();
        mymap.Dispose();

        PictureInfo record = new PictureInfo();
        record.FileName = txtAvatarName.Text;
        record.Description = TextBoxDescription.Text;
        record.Category = "User Avatar";
        record.Serverfilename = filename + ext;
        record.Uploadtime = DateTime.Now;
        record.Uploader = Convert.ToString(Session["userid"]);
        lblMessage.Text = "File " + record.FileName + " was uploaded.";

        string lsProfileAvatar = "pictures/avatar/" + csUser+ "_avatar" + ext;
        UserProfileManagement.UpdateUserProfileAvatar(csUser, lsProfileAvatar);
    }
}
