using ActionGame.GameResources.Models;
using GameMaker.Models;
using System;
using System.Windows.Forms;


namespace GameMaker.UI
{
    public partial class WorldPositionSelectDialog : Form
    {
        private Session session;
        public WorldPosition WorldPosition { get; set; }


        private WorldPositionSelectDialog()
        {

        }


        public WorldPositionSelectDialog(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.WorldPosition = new WorldPosition()
            {
                WorldMapKey = Int32.Parse(WorldList.SelectedValue.ToString()),
                WorldCellX = WorldCellSelect.SelectedCell.WorldX,
                WorldCellY = WorldCellSelect.SelectedCell.WorldY,
                PositionX = WorldTileSelect.SelectedTileX.Value,
                PositionY = WorldTileSelect.SelectedTileY.Value
            };

            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        private void WorldLocationSelectDialog_Load(object sender, EventArgs e)
        {
            WorldList.ValueMember = "Key";
            WorldList.DisplayMember = "Name";
            WorldList.DataSource = this.session.Worlds;
            WorldList.Refresh();

            WorldTileSelect.EditMode = null;
            WorldTileSelect.TileSize = 32;
            WorldTileSelect.Session = this.session;

            if(this.WorldPosition != null)
            {
                WorldList.SelectedValue = this.WorldPosition.WorldMapKey;

                foreach(WorldDefinition world in this.session.Worlds)
                {
                    if(world.Key == this.WorldPosition.WorldMapKey)
                    {
                        foreach (WorldCellDefinition cell in world.Cells)
                        {
                            if((cell.WorldX == this.WorldPosition.WorldCellX) && (cell.WorldY == this.WorldPosition.WorldCellY))
                            {
                                WorldCellSelect.SelectedCell = cell;
                                WorldTileSelect.WorldCell = cell;
                            }
                        }
                    }
                }

                WorldTileSelect.SelectedTileX = this.WorldPosition.PositionX;
                WorldTileSelect.SelectedTileY = this.WorldPosition.PositionY;
            }
        }

        private void WorldList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 worldKey = Int32.Parse(WorldList.SelectedValue.ToString());
            WorldDefinition world = null;

            foreach(WorldDefinition w in this.session.Worlds)
            {
                if(w.Key == worldKey)
                {
                    world = w;
                }
            }

            WorldCellSelect.World = world;
            WorldCellSelect.SelectedCell = null;
            WorldCellSelect.Refresh();

            WorldTileSelect.WorldCell = null;
            WorldTileSelect.Refresh();
        }

        private void WorldCellSelect_MouseClick(object sender, MouseEventArgs e)
        {
            WorldCellDefinition worldCell = WorldCellSelect.SelectedCell;

            WorldTileSelect.SelectedTileX = null;
            WorldTileSelect.SelectedTileY = null;
            WorldTileSelect.WorldCell = worldCell;
            WorldTileSelect.Refresh();
        }

    }
}
