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 CharacterSetSelectDialog : Form
    {
        public Int32 CharacterSetKey { get; set; }
        private Session session;


        public CharacterSetSelectDialog(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.CharacterSetKey = ((KeyValuePair<Int32,String>)CharacterSetList.SelectedItem).Key;
            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        private void CharacterSetList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 charsetKey = ((KeyValuePair<Int32, String>)CharacterSetList.SelectedItem).Key;
            String filePath = "";

            foreach (CharacterSetDefinition characterSet in this.session.CharacterSets)
            {
                if (characterSet.Key == charsetKey)
                {
                    foreach (RawResource t in this.session.Textures)
                    {
                        if (t.Key == characterSet.TextureKey) filePath = t.FilePath;
                    }
                }
            }

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

        private void CharacterSetSelectDialog_Load(object sender, EventArgs e)
        {
            foreach(CharacterSetDefinition characterSet in this.session.CharacterSets)
            {
                RawResource texture  =null;

                foreach (RawResource t in this.session.Textures)
                {
                    if (t.Key == characterSet.TextureKey) texture = t;
                }

                KeyValuePair<Int32, String> obj = new KeyValuePair<Int32, String>(characterSet.Key, texture.FilePath);
                CharacterSetList.Items.Add(obj);

                if(obj.Key == this.CharacterSetKey)
                {
                    CharacterSetList.SelectedItem = obj;
                }
            }

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

            if (CharacterSetList.SelectedItem == null) CharacterSetList.SelectedIndex = 0;
            CharacterSetList_SelectedIndexChanged(null, null);
        }


    }
}
