using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace SGSplitFileNamespace {

    /// <summary>
    ///    Summary description for SplitFile.
    /// </summary>
    public class SplitFile : System.Windows.Forms.Form {

        /// <summary>
        ///    Required by the Win Forms designer
        /// </summary>
	private System.ComponentModel.Container components;
   	private System.Windows.Forms.Label label4;
	private System.Windows.Forms.Button button4;
   	private System.Windows.Forms.Button button3;
    private System.Windows.Forms.TextBox textBox2;
   	private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox1;
   	private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button button2;

   	private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.GroupBox groupBox1;

   	private FileInfo f = null;  					// The file concern
   	private int x = 1; 							// number for splits
    private long len = 0;						// the lengt of the original file
   	private bool splitFile = false;				// splitfile = true, mergefile = false
    private int eachSize = 0;					// max size of each file
    private string outputName = "output.sg";	// the output of merge file

        public SplitFile() {
            InitializeComponent();
        }

        /// <summary>
        ///    Clean up any resources being used
        /// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


        /// <summary>
        ///    The main entry point for the application.
        /// </summary>
        public static void Main(string[] args) {
            Application.Run(new SplitFile());
        }


        /// <summary>
        ///    Required method for Designer support - do not modify
        ///    the contents of this method with an editor
        /// </summary>
        private void InitializeComponent(){
		this.components = new System.ComponentModel.Container();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.textBox1 = new System.Windows.Forms.TextBox();
		this.label3 = new System.Windows.Forms.Label();
		this.label4 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.button1 = new System.Windows.Forms.Button();
		this.textBox2 = new System.Windows.Forms.TextBox();
		this.button4 = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.button3 = new System.Windows.Forms.Button();
		this.button2 = new System.Windows.Forms.Button();

		//@design this.TrayHeight = 0;
		//@design this.TrayLargeIcon = false;
		//@design this.TrayAutoArrange = true;
		groupBox1.Location = new System.Drawing.Point(32, 24);
		groupBox1.TabIndex = 1;
		groupBox1.TabStop = false;
		groupBox1.Text = "Main";
		groupBox1.Size = new System.Drawing.Size(312, 80);

		textBox1.Location = new System.Drawing.Point(144, 136);
		textBox1.TabIndex = 3;
		textBox1.Size = new System.Drawing.Size(200, 20);
		textBox1.Visible = false;

		label3.Location = new System.Drawing.Point(32, 176);
		label3.Size = new System.Drawing.Size(88, 28);
		label3.TabIndex = 4;
		label3.Visible = false;

		label4.Location = new System.Drawing.Point(32, 216);
		label4.Size = new System.Drawing.Size(312, 24);
		label4.TabIndex = 8;

		label2.Location = new System.Drawing.Point(32, 136);
		label2.Size = new System.Drawing.Size(88, 28);
		label2.TabIndex = 2;
		label2.Visible = false;

		button1.Location = new System.Drawing.Point(120, 32);
		button1.Size = new System.Drawing.Size(80, 24);
		button1.TabIndex = 1;
		button1.Text = "Merge Files";
		button1.Click += new System.EventHandler(button1_Click);

		textBox2.Location = new System.Drawing.Point(144, 176);
		textBox2.TabIndex = 5;
		textBox2.Size = new System.Drawing.Size(200, 20);
		textBox2.Visible = false;
		textBox2.TextChanged += new System.EventHandler(textBox2_TextChanged);

		button4.Location = new System.Drawing.Point(144, 256);
		button4.Size = new System.Drawing.Size(75, 23);
		button4.TabIndex = 7;
		button4.Enabled = false;
		button4.Text = "Clear";
		button4.Click += new System.EventHandler(button4_Click);

		label1.Location = new System.Drawing.Point(24, 40);
		label1.Text = "Please select :";
		label1.Size = new System.Drawing.Size(80, 24);
		label1.TabIndex = 0;

		button3.Location = new System.Drawing.Point(32, 256);
		button3.Size = new System.Drawing.Size(75, 23);
		button3.TabIndex = 6;
		button3.Enabled = false;
		button3.Text = "Proceed";
		button3.Click += new System.EventHandler(button3_Click);

		button2.Location = new System.Drawing.Point(213, 32);
		button2.Size = new System.Drawing.Size(75, 23);
		button2.TabIndex = 2;
		button2.Text = "Split File";
		button2.Click += new System.EventHandler(button2_Click);
		this.Text = "Say Gin\'s SplitFile";
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.ClientSize = new System.Drawing.Size(376, 309);

		groupBox1.Controls.Add(button2);
		groupBox1.Controls.Add(button1);
		groupBox1.Controls.Add(label1);
		this.Controls.Add(label4);
		this.Controls.Add(button4);
		this.Controls.Add(button3);
		this.Controls.Add(textBox2);
		this.Controls.Add(label3);
		this.Controls.Add(textBox1);
		this.Controls.Add(label2);
		this.Controls.Add(groupBox1);
	}

    protected void textBox2_TextChanged(object sender, System.EventArgs e)	{
		// Calculate the max size of each file
		if(checkInput()){
			if(splitFile){
				len = f.Length ;
				eachSize = (int) Math.Ceiling((double) len/x);
				label4.Visible = true;
				label4.Text = "The max size of each file is approx "+eachSize+" kb";
			}
			button3.Enabled = true;
		}
	}

    protected void button4_Click(object sender, System.EventArgs e)	{
		// Clear
		resetAll();
		label4.Visible = false;
		label4.Text = "";
	}

    protected void button3_Click(object sender, System.EventArgs e)	{
		// Proceed
		if(splitFile){
			FileStream inFile = new FileStream(f.ToString(),FileMode.OpenOrCreate, FileAccess.Read);
			for (int i = 0; i<x ; i++){
				FileStream outFile = new FileStream(i+".sg",FileMode.OpenOrCreate, FileAccess.Write);
    	 		int data=0;

    	 		byte [] buffer = new byte [eachSize];
    		 	if ((data=inFile.Read(buffer,0,eachSize))>0){
					outFile.Write(buffer,0,data);
	  			}
		  		outFile.Close();
		  	}
			inFile.Close();
			label4.Visible = true;
			label4.Text = "SplitFile completed sucessfully";
		}
		else{
			FileStream outFile = new FileStream(outputName,FileMode.OpenOrCreate, FileAccess.Write);

			for (int i=0;i<x; i++){
    	 		int data=0;
			   	byte [] buffer = new byte [1024];
				FileStream inFile = new FileStream(i+".sg",FileMode.OpenOrCreate, FileAccess.Read);
    			while ((data=inFile.Read(buffer,0,1024))>0){
					outFile.Write(buffer,0,data);
	  			}
	  			inFile.Close();
			}
			outFile.Close();
			label4.Visible = true;
			label4.Text = "MergeFile completed sucessfully";
		}
	}

    protected void button2_Click(object sender, System.EventArgs e)	{
		// Split
		button1.Enabled = false;
		button2.Enabled = false;
		button4.Enabled = true;

		label2.Visible = true;
		label2.Text = "Name of file to split :";
		textBox1.Visible = true;
		textBox1.Text = "";
		textBox1.Enabled = true;

		label3.Visible = true;
		label3.Text = "How many files to split into :";
		textBox2.Visible = true;
		textBox2.Text = "";
		splitFile = true;
	}

    protected void button1_Click(object sender, System.EventArgs e)	{
		// Merge
		button1.Enabled = false;
		button2.Enabled = false;
		button4.Enabled = true;

		label2.Visible = true;
		label2.Text = "Name of the first file :";
		textBox1.Visible = true;
		textBox1.Text = "0.sg";
		textBox1.Enabled = false;

		label3.Visible = true;
		label3.Text = "How many files to merge :";
		textBox2.Visible = true;
		textBox2.Text = "";

		splitFile = false;
	}

	private bool checkInput(){
		if (textBox1.Text.Length ==0 || textBox2.Text.Length ==0){
			label4.Visible = true;
			label4.Text = "The input(s) must not be blank";
			return false;
		}

		try{
			x = Int32.Parse(textBox2.Text);
		}
		catch(Exception ex){
			label4.Visible = true;
			label4.Text = "The second textfield must be a number";
			Console.WriteLine("The exception => "+ex);
			return false;
		}

		f = new FileInfo(textBox1.Text);
		if(!f.Exists){
			label4.Visible = true;
			label4.Text = "The file "+textBox1.Text+" does not exist";
			f = null;
			return false;
		}
		return true;
	}

	private void resetAll(){
		button1.Enabled = true;
		button2.Enabled = true;
		button3.Enabled = false;
		button4.Enabled = false;

		label2.Visible = false;
		label3.Visible = false;
		label2.Text = "";
		label3.Text = "";

		textBox1.Visible = false;
		textBox1.Text = "";
		textBox2.Visible = false;
		textBox2.Text = "";
	}
    }
}
