using System;
using System.IO;
using System.Web.UI;
using System.Web;

namespace ExWeb
{
    public partial class Mod_Download : Mod
    {
        public static string Help_Description =
            "Download einer Datei";
        public static string[][] Help_Attributes = new string[][]
        {
            // Attribut, Wert, DefaultWert, Optional, Hilfe
            new string[] {"Fil", "string", "","opt","Name der Datendatei","Fil=\"Calendar.txt\""},
            new string[] {"Pat","path","/","opt","Verzeichnis","Pat=\"/meineBilder\""},
            new string[] {"Name","string","","opt","Wird statt des Dateinmens angezeigt","Name=\"Bild1\""},
            new string[] {"Description","string","","opt","Weitergehende Beschreibung","Description=\"147 Seiten, englisch, PDF\""},
        };
        protected string _Fil = "";
        public string Fil
        {
            set
            {
                _Fil = value;
            }
        }
        protected string _Pat = "/";
        public string Pat
        {
            set
            {
                if (!value.EndsWith("/")) value += "/";
                _Pat = value;
            }
        }
        string _Name = "";
        public string Name
        {
            set
            {
                _Name = value;
            }
        }
        string _Description = "";
        public string Description
        {
            set
            {
                _Description = value;
            }
        }
        long size = 0;
        string PathToDownloadAbsolute = "";

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            SetPanelLayout(this.Controls);
        }
        protected new void Page_Load(object sender, EventArgs e)
        {
            Page.LoadComplete += new EventHandler(Page_LoadComplete);

            Fil = Mod.GetQs_String("fil", _Fil, this.Page);
            Pat = Mod.GetQs_String("pat",_Pat, this.Page);
            Name = Mod.GetQs_String("name", _Name, this.Page);
            Description = Mod.GetQs_String("description", _Description, this.Page);

            PathToDownloadAbsolute = Server.MapPath(_Pat);
            _Pat = (!_Pat.StartsWith("/") ? "/" : "") + _Pat + (!_Pat.EndsWith("/") ? "/" : "");

            if (_Fil == "")
            {
                Panel_Module.Visible = false;
                return;
            }
            else
            {
                if (!PathToDownloadAbsolute.EndsWith("\\")) PathToDownloadAbsolute += "\\";
                PathToDownloadAbsolute += _Fil;
                FileInfo f = new FileInfo(PathToDownloadAbsolute);
                if (f.Exists)
                {
                    size = f.Length;

                    Label_Object.Text = (_Name == "" ? _Fil : _Name);
                    Label_Object.ToolTip = _Pat + _Fil;
                    Label_Size.Text = GetSize(size);
                    Label_Description.Text = _Description;
                }
                else
                {
                    Panel_Module.Visible = false;
                    return;
                }
            }
        }
        protected string GetSize(long size)
        {
            if (size < 1024) return size.ToString();
            if (size < (1024 * 1024)) return (size / 1024).ToString("#.0") + "k";
            return (size / (1024*1024)).ToString("#.#") + "M";
        }
        protected void MenuDownload(string path)
        {
            FileInfo downloadFile = new FileInfo(path);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", downloadFile.Name));
            HttpContext.Current.Response.AddHeader("Content-Length", downloadFile.Length.ToString());
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.WriteFile(downloadFile.FullName);
            HttpContext.Current.Response.End();
        }

        protected void Button_Download_Click(object sender, EventArgs e)
        {
            MenuDownload(PathToDownloadAbsolute);
        }
    }
}