using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.IO; 
//using System.Diagnostics;
using fomm.Scripting; 

class Script : FalloutNewVegasBaseScript {
	// GUI components
	public static Form frm;
	public static PictureBox pic;
	public static Panel pnl;
	public static Label lblOptions;
	public static Label lbllang;
	public static ComboBox cbxlang;
	public static Label lblDesc;
	public static Button btnInstall;
	public static Button btnCancel;
	
	// proceed with installation?
	public static bool install = false;
	
	public static void SaveFomodFileAs(string filename, string newFilename) {
		byte[] data = GetFileFromFomod(filename);
		GenerateDataFile(newFilename, data);
	}
	
	public static Image GetImageFromFomod(string filename) {
		byte[] data = GetFileFromFomod(filename);
		MemoryStream s = new MemoryStream(data);
		Image img = Image.FromStream(s);
		s.Close();		
		return img;
	}

	public static void CreateInstallationForm() {
		// create form
		frm = CreateCustomForm();
		
		// center, resize and change border of form
		frm.FormBorderStyle = FormBorderStyle.None;
		frm.StartPosition = FormStartPosition.CenterScreen;
		frm.Size = new Size(600, 435);
		
		// place background picture box
		pic = new PictureBox();
		pic.Location = new Point(0,0);
		pic.Size = new Size(600, 435);
		
		// load picture file from .fomod and stream into picture box
		pic.Image = GetImageFromFomod("fomod/DD.png");

		// place background picture on form		
		frm.Controls.Add(pic);

		// create options panel that will contain controls
		pnl = new Panel();
		//pnl.BackColor = SystemColors.ButtonHighlight;
		pnl.Location = new Point(12, 300);
		pnl.Size = new Size(576, 65);
		
		// place controls on panel
		lblOptions = new Label();
		lblOptions.Text = "Deadly Dragons Installation";
		lblOptions.Font = new Font("Arial", 10, FontStyle.Bold);
		lblOptions.Location = new Point(187, 5);
		lblOptions.AutoSize = true;
		pnl.Controls.Add(lblOptions);
		
		lbllang = new Label();
		lbllang.Text = "Deadly Dragons";
		lbllang.Font = new Font("Arial", 9, FontStyle.Bold);
		lbllang.Location = new Point(157, 32);
		lbllang.AutoSize = true;
		pnl.Controls.Add(lbllang);

		cbxlang = new ComboBox();
		cbxlang.Location = new Point(277, 30);
		cbxlang.Size = new Size(149, 21);
		cbxlang.Items.Add("Normal");
		cbxlang.Items.Add("Loremonger");
		cbxlang.DropDownStyle = ComboBoxStyle.DropDownList;
		cbxlang.SelectedIndex = 0;
		pnl.Controls.Add(cbxlang);


		
		// place details controls
		//lblDesc = new Label();
		//lblDesc.Location = new Point(11, 202);
		//lblDesc.Size = new Size(253, 200);
		//lblDesc.AutoSize = false;
		//lblDesc.TextAlign = ContentAlignment.MiddleCenter;
		//lblDesc.Text = "Selection description";
		//pnl.Controls.Add(lblDesc);
		
		// place panel on picture box
		pic.Controls.Add(pnl);
		
		// create buttons
		btnInstall = new Button();
		btnInstall.Text = "Install";
		btnInstall.Font = new Font("Arial", 12, FontStyle.Bold);
		btnInstall.Location = new Point(470, 368);
		btnInstall.Size = new Size(120, 60);
		
		btnCancel = new Button();
		btnCancel.Text = "Cancel";
		btnCancel.Font = new Font("Arial", 12, FontStyle.Bold);
		btnCancel.Location = new Point(10, 368);
		btnCancel.Size = new Size(120, 60);

		// add buttons to background
		pic.Controls.Add(btnInstall);
		pic.Controls.Add(btnCancel);
		// attach event handlers
		AttachHandlers();

		// set default info
	//	lblDesc.Text = "Deadly Dragons is a mod with a singular purpose; make dragons the feared & powerful creatures they are supposedly are.";
	}

	public static void AttachHandlers() {
        //Attach a handler that will fire when the apply button is clicked 
        btnInstall.Click += delegate(object sender, EventArgs args) 
        { 
        	install = true;
			frm.Close();
        };

        btnCancel.Click += delegate(object sender, EventArgs args) 
        { 
			frm.Close();
        };   
        
      }
	
	public static void DoInstallation() {

		CopyDataFile("bsa/DeadlyDragons.bsa","DeadlyDragons.bsa");

        if (cbxlang.SelectedIndex == 0) {
        	InstallFileFromFomod("normal/DeadlyDragons.esp", "DeadlyDragons.esp");
            SetPluginActivation("DeadlyDragons.esp", true);
		} else if (cbxlang.SelectedIndex == 1) {
        	InstallFileFromFomod("loremonger/DeadlyDragons.esp", "DeadlyDragons.esp");
            SetPluginActivation("DeadlyDragons.esp", true);
		}
          //MessageBox("Don't forget to endorse ;)", "Have fun!");
	}

	
	public static bool OnActivate() {     
		CreateInstallationForm();
		frm.ShowDialog();
		
		if(install) {
			DoInstallation();
		}
		
		return install;
	}
}
