using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using System.Collections;


namespace SpriteLibrary
{
    internal partial class SpriteEntryForm : Form
    {
        SpriteController MyController;
        ResourceManager myResources = null;
        List<SpriteInfo> SpriteInformation = new List<SpriteInfo>();
        SpriteInfo TempInformation = null;
        Size SnapGridSize = new Size(5,5);
        SpriteDatabase myDatabase = null;
        int CurrentSIIndex = -1;  //The information item we are editing.  -1 means it is a new one.
        int CurrentSIAnimation = -1;

        internal SpriteEntryForm(SpriteDatabase theDatabase, List<SpriteInfo> ListToWorkOn, Size GridSize)
        {
            InitializeComponent();
            myDatabase = theDatabase;
            myResources = myDatabase.GetResourceManager();
            SnapGridSize = GridSize;
            LocalSetup();
            SpriteInformation.AddRange(ListToWorkOn);
        }

        private void LocalSetup()
        {
            pbImageField.BackgroundImageLayout = ImageLayout.Stretch;
            MyController = new SpriteController(pbImageField);
            UpdateMenu();
            PopulateMenu();
        }

        internal List<SpriteInfo> GetUpdatedList()
        {
            return SpriteInformation;
        }

        private void PopulateMenu()
        {
            ResourceManager rm = myResources;
            PopulateMenu(rm);

        }

        private void PopulateMenu(ResourceManager rm)
        {
            if (myResources == null) myResources = rm;
            ResourceSet RS = rm.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
            cbStartingImage.Items.Clear();
            foreach (DictionaryEntry entry in RS)
            {
                string resourceKey = entry.Key.ToString();
                object resource = entry.Value;
                if (resource is Image)
                {
                    cbStartingImage.Items.Add(resourceKey);
                }
            }
            cbStartingImage.SelectedIndex = 0;
        }

        private void UpdateMenu()
        {
            lblCountSprites.Text = SpriteInformation.Count.ToString();
            if (TempInformation == null) SetUpEmptyInfo();
        }

        private void SetUpEmptyInfo()
        {
            TempInformation = new SpriteInfo();
            TempInformation.SpriteName = "";
            TempInformation.ViewPercent = 100;
            AnimationInfo AI = new AnimationInfo();
            AI.AnimSpeed = 200;
            AI.FieldsToUse = AnimationType.SpriteDefinition;
            AI.Height = 100;
            AI.Width = 100;
            AI.StartPoint = new Point(0, 0);
            AI.ViewPercent = 100;
            TempInformation.Animations.Add(AI);
        }

        private void cbStartingImage_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResourceManager rm;
            if (myResources != null) rm = myResources;
            else rm = Properties.Resources.ResourceManager;
            //We have a selected item
            if (cbStartingImage.SelectedIndex >= 0)
            {
                //Load in a new image into our background
                Image NewImage = (Bitmap)rm.GetObject(cbStartingImage.SelectedItem.ToString());
                if (NewImage != null)
                {
                    MyController.ReplaceOriginalImage(new Bitmap(NewImage));
                    pbImageField.BackgroundImage = new Bitmap(NewImage);
                    pbImageField.Invalidate();
                }
            }
        }

        private void SpriteEntryForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            myDatabase.Save(); //try saving the file
        }
    }
}
