using ActionGame.GameResources.Models;
using ActionGame.RawResources.Models;
using GameMaker.Logic;
using GameMaker.Models;
using System;
using System.Collections.Generic;
using System.Windows.Forms;


namespace GameMaker.UI
{
    public partial class EnemySelectDialog : Form
    {
        public Int32 EnemyKey { get; set; }
        private Session session;


        public EnemySelectDialog(Session session)
        {
            InitializeComponent();

            this.session = session; 
        }

        private void CancelDialogButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }

        private void ConfirmDialogButton_Click(object sender, EventArgs e)
        {
            this.EnemyKey = ((KeyValuePair<Int32,String>)EnemyList.SelectedItem).Key;
            this.DialogResult = DialogResult.OK;
            this.Hide();
        }


        private void EnemyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 enemyKey = ((KeyValuePair<Int32, String>)EnemyList.SelectedItem).Key;
            String filePath = "";

            foreach (EnemyDefinition enemy in this.session.Enemies)
            {
                if (enemy.Key == enemyKey)
                {
                    foreach(CharacterSetDefinition characterSet in this.session.CharacterSets)
                    {
                        if(characterSet.Key == enemy.CharacterSetKey)
                        {
                            foreach (RawResource texture in this.session.Textures)
                            {
                                if (texture.Key == characterSet.TextureKey) filePath = texture.FilePath;
                            }
                        }
                    }
                }
            }

            PreviewBox.ImageLocation = RawResourceHelper.GetFilePathForTexture(this.session.WorkingDirectory, filePath);
            PreviewBox.SizeMode = PictureBoxSizeMode.Zoom;
        }


        private void CharacterSetSelectDialog_Load(object sender, EventArgs e)
        {
            foreach(EnemyDefinition enemy in this.session.Enemies)
            {
                KeyValuePair<Int32, String> obj = new KeyValuePair<Int32, String>(enemy.Key, enemy.Name);
                EnemyList.Items.Add(obj);

                if (obj.Key == this.EnemyKey)
                {
                    EnemyList.SelectedItem = obj;
                }
            }
            

            EnemyList.ValueMember = "Key";
            EnemyList.DisplayMember = "Value";

            if (EnemyList.SelectedItem == null) EnemyList.SelectedIndex = 0;
            EnemyList_SelectedIndexChanged(null, null);
        }


    }
}
