using System;
using System.IO;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using ASC.Api.DocumentConverter;

namespace OnlineEditorsExample
{
    public partial class DocEditor : Page
    {
        public static string FileName;

        public static string FileUri
        {
            get
            {
                var result = _Default.FileUri(FileName);
                return result;
            }
        }

        protected string Key
        {
            get
            {
                var result = ServiceConverter.GenerateRevisionId(_Default.CurUserHostAddress + "/" + Path.GetFileName(FileUri));
                return result;
            }
        }

        protected string ValidateKey
        {
            get
            {
                var result = ServiceConverter.GenerateValidateKey(Key);
                return result;
            }
        }

        protected string DocServiceApiUri
        {
            get
            {
                var result = WebConfigurationManager.AppSettings["files.docservice.url.api"] ?? string.Empty;
                return result;
            }
        }

        protected string CallBackUrl
        {
            get
            {
                var result = "http://" + Request.Url.Authority + "/FileSave.ashx?filename=" + Server.UrlEncode(FileName);
                return result;
            }
        }

        protected string DocumentType
        {
            get
            {
                var ext = Path.GetExtension(FileName).ToLower();

                if (FileType.ExtsDocument.Contains(ext)) return "text";
                if (FileType.ExtsSpreadsheet.Contains(ext)) return "spreadsheet";
                if (FileType.ExtsPresentation.Contains(ext)) return "presentation";

                return string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var externalUrl = Request["fileUrl"];
            if (!string.IsNullOrEmpty(externalUrl))
            {
                FileName = _Default.DoUpload(externalUrl);
            }
            else
            {
                FileName = Request["fileID"];
            }
            
            //var type = Request["type"];
            //if (!string.IsNullOrEmpty(type))
            //{
            //    Try(type);
            //    Response.Redirect("doceditor.aspx?fileID=" + HttpUtility.UrlEncode(FileName));
            //}
        }

        private static void Try(string type)
        {
            string ext;
            switch (type)
            {
                case "document":
                    ext = ".docx";
                    break;
                case "spreadsheet":
                    ext = ".xlsx";
                    break;
                case "presentation":
                    ext = ".pptx";
                    break;
                default:
                    return;
            }
            var demoName = "demo" + ext;
            FileName = _Default.GetCorrectName(demoName);

            File.Copy(HttpRuntime.AppDomainAppPath + "app_data/" + demoName, _Default.StoragePath + FileName);
        }
    }
}