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

using Icarus.Logic;
using Icarus.Graphics.Animation;

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

        #region Declarations

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textBoxStageName;
        private System.Windows.Forms.Button buttonRemove;
        private System.Windows.Forms.Button buttonAdd;
        private System.Windows.Forms.TextBox textBoxParallaxNum;
        private System.Windows.Forms.Button buttonSetParallax;
        private System.Windows.Forms.Button buttonSaveStage;
        private System.Windows.Forms.ListBox listBoxStages;

        /// <summary>
        /// The stage being edited by this form.
        /// </summary>
        private Icarus.Logic.Stage stage;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;

        private Icarus.Logic.LevelBackground lvlbgSelected;

        #endregion

        #region Construction

		public FormStageEdit()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

		}

        #endregion

        #region Disposal

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

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		 
        #endregion
		
        
        private void InitializeComponent()
		{
            this.listBoxStages = new System.Windows.Forms.ListBox();
            this.textBoxStageName = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.buttonRemove = new System.Windows.Forms.Button();
            this.buttonAdd = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.textBoxParallaxNum = new System.Windows.Forms.TextBox();
            this.buttonSetParallax = new System.Windows.Forms.Button();
            this.buttonSaveStage = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            // 
            // listBoxStages
            // 
            this.listBoxStages.Location = new System.Drawing.Point(8, 64);
            this.listBoxStages.Name = "listBoxStages";
            this.listBoxStages.Size = new System.Drawing.Size(168, 108);
            this.listBoxStages.TabIndex = 0;
            this.listBoxStages.Click += new System.EventHandler(this.listBoxStages_Click);
            // 
            // textBoxStageName
            // 
            this.textBoxStageName.Location = new System.Drawing.Point(80, 8);
            this.textBoxStageName.Name = "textBoxStageName";
            this.textBoxStageName.TabIndex = 1;
            this.textBoxStageName.Text = "textBox1";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(72, 23);
            this.label1.TabIndex = 2;
            this.label1.Text = "Stage Name:";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 48);
            this.label2.Name = "label2";
            this.label2.TabIndex = 3;
            this.label2.Text = "Images:";
            // 
            // buttonRemove
            // 
            this.buttonRemove.Location = new System.Drawing.Point(184, 96);
            this.buttonRemove.Name = "buttonRemove";
            this.buttonRemove.TabIndex = 4;
            this.buttonRemove.Text = "Remove";
            this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
            // 
            // buttonAdd
            // 
            this.buttonAdd.Location = new System.Drawing.Point(184, 64);
            this.buttonAdd.Name = "buttonAdd";
            this.buttonAdd.TabIndex = 5;
            this.buttonAdd.Text = "Add";
            this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 184);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(144, 16);
            this.label3.TabIndex = 6;
            this.label3.Text = "Selected Image Parallax #:";
            // 
            // textBoxParallaxNum
            // 
            this.textBoxParallaxNum.Location = new System.Drawing.Point(144, 176);
            this.textBoxParallaxNum.Name = "textBoxParallaxNum";
            this.textBoxParallaxNum.Size = new System.Drawing.Size(32, 20);
            this.textBoxParallaxNum.TabIndex = 7;
            this.textBoxParallaxNum.Text = "";
            // 
            // buttonSetParallax
            // 
            this.buttonSetParallax.Location = new System.Drawing.Point(184, 176);
            this.buttonSetParallax.Name = "buttonSetParallax";
            this.buttonSetParallax.TabIndex = 8;
            this.buttonSetParallax.Text = "Set Parallax #";
            this.buttonSetParallax.Click += new System.EventHandler(this.buttonSetParallax_Click);
            // 
            // buttonSaveStage
            // 
            this.buttonSaveStage.Location = new System.Drawing.Point(184, 256);
            this.buttonSaveStage.Name = "buttonSaveStage";
            this.buttonSaveStage.TabIndex = 9;
            this.buttonSaveStage.Text = "Save Stage";
            this.buttonSaveStage.Click += new System.EventHandler(this.buttonSaveStage_Click);
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(8, 208);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(240, 40);
            this.label4.TabIndex = 10;
            this.label4.Text = "The first image added will be displayed in the level editor and be used to set th" +
                "e autoscroll width and height for a player\'s movements.";
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.Filter = "PNG files (*.png)|*.png|JPEG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp|All f" +
                "iles (*.*)|*.*";
            this.openFileDialog1.RestoreDirectory = true;
            // 
            // FormStageEdit
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(264, 288);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.buttonSaveStage);
            this.Controls.Add(this.buttonSetParallax);
            this.Controls.Add(this.textBoxParallaxNum);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.buttonAdd);
            this.Controls.Add(this.buttonRemove);
            this.Controls.Add(this.listBoxStages);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBoxStageName);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "FormStageEdit";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Stage Editor";
            this.ResumeLayout(false);

        }
		


        #endregion

        #region Properties

        /// <summary>
        /// gets or sets the stage this form is editing
        /// </summary>
        public Icarus.Logic.Stage Stage
        {
            get { return this.stage; }
            set 
            { 
                this.stage = value;
                textBoxStageName.Text = this.stage.StageName;
                populateStageList();
            }
        }   

        #endregion

        #region Methods


        private void buttonAdd_Click(object sender, System.EventArgs e)
        {
            String strBackgroundImagePath;

            //Ask for background image file
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Set image filepath in stage in level format
                strBackgroundImagePath = openFileDialog1.FileName;
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(strBackgroundImagePath);
                System.IO.DirectoryInfo di2 = new System.IO.DirectoryInfo(Icarus.Resources.ResourceManager.Instance.ImagePath);
                strBackgroundImagePath = di.FullName.Replace(di2.FullName, "");

                //Create background:
                Icarus.Logic.LevelBackground bg = new Icarus.Logic.LevelBackground();
				bg.SetAnimation(strBackgroundImagePath, AnimationType.LoopForever, 1);
                bg.ParallaxFactor = 1.0;
                stage.AddBackground(bg);

                // Update stage dimensions:
                if(stage.Backgrounds.Length <= 1)
                {
                    stage.Width = bg.Animation.CurrentFrame.GetSize().Width;
                    Stage.Height = bg.Animation.CurrentFrame.GetSize().Height;
                }

                populateStageList();
                //lbStages.Items.Add(di.Name);

            }
        }

        private void populateStageList()
        {
            listBoxStages.Items.Clear();
            for(int i = 0 ; i < stage.GetBackgroundCount(); i++)
            {
                listBoxStages.Items.Add(stage.Backgrounds[i].Animation.FramesetPath);
            }
        }

        private void listBoxStages_Click(object sender, System.EventArgs e)
        {
            for(int i = 0; i < stage.GetBackgroundCount(); i++)
            {
                if(listBoxStages.Text.Equals(stage.Backgrounds[i].Animation.FramesetPath))
                {
                    lvlbgSelected = stage.Backgrounds[i];
                    textBoxParallaxNum.Text = lvlbgSelected.ParallaxFactor.ToString();
                    break;
                }
            } 
            
        }

        private void buttonSetParallax_Click(object sender, System.EventArgs e)
        {
            lvlbgSelected.ParallaxFactor = Convert.ToDouble(textBoxParallaxNum.Text);
        }

        private void buttonRemove_Click(object sender, System.EventArgs e)
        {
            for(int i = 0; i < stage.GetBackgroundCount(); i++)
            {
                if(listBoxStages.Text.Equals(stage.Backgrounds[i].Animation.FramesetPath))
                {
                    stage.removeBackground(stage.Backgrounds[i]);
                    populateStageList();
                    break;
                }
            } 
        }



        private void buttonSaveStage_Click(object sender, System.EventArgs e)
        {
            if(textBoxStageName.Text.Equals("NoName"))
            {
                //Display error message
                MessageBox.Show("Please enter a name for this stage.", "Name Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                return;
            }

            if(stage.Backgrounds.Length < 1)
            {
                //Display error message
                MessageBox.Show("Please add at least one background.", "No Background Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                return;
            }

            stage.StageName = textBoxStageName.Text;
            this.Close();

        }

        #endregion
 
	}
}
