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


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


        private void TextureList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 textureKey = ((KeyValuePair<Int32, String>)TextureList.SelectedItem).Key;
            String filePath = "";

            foreach (RawResource texture in this.session.Textures)
            {
                if (texture.Key == 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 (RawResource texture in this.session.Textures)
            {
                KeyValuePair<Int32, String> obj = new KeyValuePair<Int32, String>(texture.Key, texture.FilePath);
                TextureList.Items.Add(obj);

                if (obj.Key == this.TextureKey)
                {
                    TextureList.SelectedItem = obj;
                }
            }

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

            if (TextureList.SelectedItem == null) TextureList.SelectedIndex = 0;
            TextureList_SelectedIndexChanged(null, null);
        }


    }
}
