﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.IO;
using OmniBean.CryptRW;

namespace BXB_Builder
{
    public partial class BuilderHome : MetroForm
    {
        List<string> bxbFileNames = new List<string>();
        public BuilderHome()
        {
            Form.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.Opacity = 0;
        }
        void FadeAway()
        {
            int duration = 1000;//in milliseconds
            int steps = 100;
            Timer timer = new Timer();
            timer.Interval = duration / steps;

            int currentStep = 0;
            timer.Tick += (arg1, arg2) =>
            {
                Opacity = 1 - ((double)currentStep) / steps;
                currentStep++;

                if (currentStep >= steps)
                {
                    timer.Stop();
                    timer.Dispose();
                    this.Hide();
                }
            };
            timer.Start();
        }
        void FadeIn()
        {
            this.Show();
            this.WindowState = FormWindowState.Minimized;
            this.WindowState = FormWindowState.Normal;
            int duration = 1000;//in milliseconds
            int steps = 100;
            Timer timer = new Timer();
            timer.Interval = duration / steps;

            int currentStep = 0;
            timer.Tick += (arg1, arg2) =>
            {
                Opacity = ((double)currentStep) / steps;
                currentStep++;

                if (currentStep >= steps)
                {
                    timer.Stop();
                    timer.Dispose();
                }
            };
            timer.Start();
        }

        private void BuilderHome_Load(object sender, EventArgs e)
        {
            FadeIn();
        }

        void RefreshList()
        {
            listBox1.Items.Clear();
            foreach (string fn in bxbFileNames)
            {
                string it = Path.GetFileName(fn);
                listBox1.Items.Add(it);
                comboBox1.Items.Add(it);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            var dr = ofd.ShowDialog();
            if (dr==DialogResult.OK)
            {
                foreach (string fileName in ofd.FileNames)
                {
                    bxbFileNames.Add(fileName);
                }
            }
            RefreshList();
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem == null) { MessageBox.Show("Select a main file."); return; }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".bxb";
            var dr = sfd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                label3.Text = "Building BXB...";
                await Task.Run(()=> BuildBXB(sfd.FileName));
                label3.Text = "Done.";
            }
        }
        void BuildBXB(string ofp)
        {
            string mainFileName = (string)comboBox1.SelectedItem;
            //MessageBox.Show(mainFileName);
            string args = textBox2.Text;
            CryptRWArchive crw = new CryptRWArchive(ofp, "bxb");
            foreach (string file in bxbFileNames)
            {
                CopyFileToBXB(ref crw, file, Path.GetFileName(file));
            }
            crw.WriteAllText("pkg.conf",string.Format("{0}|{1}",mainFileName,args));
            //write files
            crw.CloseArchive();
            MessageBox.Show("Your BXB has been built!");
        }
        public static void CopyFileToBXB(ref CryptRWArchive crw, string originalFile, string arcPath)
        {
            crw.WriteAllBytes(arcPath, File.ReadAllBytes(originalFile));
        }
		void ListBox1DragDrop(object sender, DragEventArgs e)
		{
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			foreach (string fileName in files)
            {
                bxbFileNames.Add(fileName);
            }
			RefreshList();
		}
		void ListBox1DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Copy;
		}
    }
}
