using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace VectorRace_LevelDesigner
{
    public partial class FieldLayout : UserControl
    {
        private Field[,] Fields;
        private List<Field> Types = new List<Field>();

        public FieldLayout()
        {
            Reset();

            InitializeComponent();
        }

        public void Reset()
        {
            Controls.Clear();

            Fields = new Field[Level.WidthSegments, Level.HeightSegments];

            for (int i = 0; i < Level.WidthSegments; i++)
            {
                for (int j = 0; j < Level.HeightSegments; j++)
                {
                    Fields[i, j] = new Field();
                    Fields[i, j].Type = 0;
                    Fields[i, j].Location = new Point(10 + 34 * i, 10 + 34 * j);
                    Fields[i, j].Size = new Size(32, 32);

                    Controls.Add(Fields[i, j]);
                }
            }

            int x = 0;

            List<int> types = new List<int>();
            types.Add(0);
            types.Add(1);
            types.Add(2);
            types.Add(3);
            types.Add(4);
            types.Add(5);
            types.Add(6);
            types.Add(11);

            foreach (int type in types)
            {
                Field field = new Field(false);
                field.Type = type;
                field.Location = new Point(10 + 34 * x, 10 + 34 * (Level.HeightSegments + 1));
                field.Size = new Size(32, 32);
                Types.Add(field);
                Controls.Add(field);

                x++;
            }
        }

        public void LoadLevel(Level level)
        {
            Reset();

            for (int i = 0; i < Level.WidthSegments; i++)
            {
                for (int j = 0; j < Level.HeightSegments; j++)
                {
                    Fields[i, j].Type = level.Fields[i, j];
                    Controls.Add(Fields[i, j]);
                }
            }
        }

        public Level GetLevel()
        {
            int[,] fields = new int[Level.WidthSegments, Level.HeightSegments];

            for (int i = 0; i < Level.WidthSegments; i++)
            {
                for (int j = 0; j < Level.HeightSegments; j++)
                {
                    fields[i, j] = Fields[i, j].Type;
                }
            }

            Level level = new Level(fields);

            return level;
        }
    }
}
