using QW2021C.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QW2021C.Administration
{
    public partial class Upload : System.Web.UI.Page
    {
        Helper.Log log = new Helper.Log();

        protected void Page_Load(object sender, EventArgs e)
        {
          

            try
            {
                if (Session["LoggedIn"].ToString() == "False")
                {
                    Response.Redirect("~/default.aspx");
                    return;
                }
            }
            catch
            {
                Response.Redirect("~/default.aspx");
                return;

            }
            if (Page.IsPostBack == true)
            {
               // if (this.Tab1.SelectedItemIndex == 1) this.target.Visible = true;

                return;
            }

            //this.ButtonVerarbeiten1.Enabled = false;
            //this.ButtonVerarbeiten2.Enabled = false;
            //this.Upload1.Enabled = false;
           
        }

        protected void Button1_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            clsDB db = new clsDB();
            string targetFolder = HttpContext.Current.Server.MapPath("~/Upload");
            db.update_verein(targetFolder);
            db.update_athlete(targetFolder);
            Response.Write("<script>alert('Daten-Upload durchgeführt')</script>");


        }

        protected void Upload1_Complete(object sender, Syncfusion.JavaScript.Web.UploadBoxCompleteEventArgs e)
        {
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Datei-Upload erfolgreich "+e.Name);
            this.Tab1.SelectedItemIndex = 1;
            //this.target.Visible = true;
            this.HiddenField1.Value = e.Name;

        }

        protected void ButtonVerarbeiten1_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Start unzip");
            unzip_file();
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Ende unzip");

            this.Tab1.SelectedItemIndex = 2;
        }

        private void load_data()
        {
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Start load data");
            clsDB db = new clsDB();
            string targetFolder = HttpContext.Current.Server.MapPath("~/Upload");
            db.update_verein(targetFolder);
            db.update_athlete(targetFolder);
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Ende load data");

        }
        private void unzip_file()/* TODO ERROR: Skipped SkippedTokensTrivia */
        {
            string filename = this.HiddenField1.Value;
            string targetFolder = HttpContext.Current.Server.MapPath("~/Upload");
            filename=targetFolder+"\\"+filename;
            string extension;
            extension = System.IO.Path.GetExtension(filename);
            if (extension == ".gz")

            {
                Helper.zipHelper csh = new Helper.zipHelper();
                var fi = new System.IO.FileInfo(filename);
                csh.Decompress(fi);
                System.Data.DataSet DS = new System.Data.DataSet();
                string fn;
                fn = System.IO.Path.GetFileNameWithoutExtension(filename);
                DS.ReadXml(targetFolder+"\\"+fn);
                System.Data.DataSet dsverein = new System.Data.DataSet();
                dsverein.Tables.Add(DS.Tables["account"].Copy());
                System.Data.DataSet dsathlete = new System.Data.DataSet();
                dsathlete.Tables.Add(DS.Tables["athlete"].Copy());
                dsverein.WriteXml(targetFolder + "\\verein.xml");
                dsathlete.WriteXml(targetFolder + "\\athlete.xml");
                this.Tab1.SelectedItemIndex = 2;
            }
        }

        protected void ButtonVerarbeiten2_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            load_data();
            this.Tab1.SelectedItemIndex = 3;
            clsDB db = new clsDB();
            db.Get_Tabledata("Select count(*) from base_athlete", false, true);
            int athleten = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0][0]);
            db.dsdaten.Tables.Clear();
            db.Get_Tabledata("Select count(*) from base_account", false, true);
            int vereine = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0][0]);
            this.lblathleten.Text = athleten.ToString();
            this.lblvereine.Text = vereine.ToString();


        }
    }
}