//-----------------------------------------------------------------------
// 
//     Copyright © 2012 Nils Hammar. All rights reserved.
// 
//-----------------------------------------------------------------------
/*
 * Software to access vehicle information via the OBD-II connector.
 * 
 * Copyright © 2012 Nils Hammar
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see .
 * 
 * Alternative licensing is possible, see the licensing document.
 * 
 * The above text may not be removed or modified.
 */
namespace Gauges
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    /// 
    /// Select which gauge to use.
    /// 
    public partial class TelltaleAreaSelector : Form
    {
        /// 
        /// Gets Selected telltale group area.
        /// 
        /// 'null' if no area selected.
        /// 
        /// 
        public uint? selectedItem { get; private set; }
        /// 
        /// Coordinates of telltale areas.
        /// 
        private static int[][] coords =
        {
            new int[] {108, 6, 165, 76},
            new int[] {170, 6, 469, 76},
            new int[] {475, 6, 530, 76},
            new int[] {290, 85, 349, 118},
            new int[] {290, 261, 349, 294},
            new int[] {7, 313, 104, 364},
            new int[] {109, 300, 530, 364},
            new int[] {535, 313, 632, 346},
        };
        /// 
        /// Current telltale area.
        /// 
        private uint currentItem;
        /// 
        /// Used telltale areas.
        /// 
        private uint[] usedItems;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// Current telltale area.
        /// Used telltale area.
        public TelltaleAreaSelector(uint currentItem, uint[] usedItems)
        {
            this.selectedItem = currentItem;
            this.currentItem = currentItem;
            this.usedItems = usedItems;
            this.InitializeComponent();
        }
        /// 
        /// Draws the pointer.
        /// 
        /// Event arguments.
        protected override void OnPaint(PaintEventArgs e)
        {
            if (e != null)
            {
                base.OnPaint(e);
                if (this.usedItems != null)
                {
                    Graphics gr = Graphics.FromImage(this.pictureBox1.Image);
                    Color c1 = Color.FromArgb(127, Color.Red);
                    Color c2 = Color.FromArgb(63, Color.Green);
                    foreach (uint item in this.usedItems)
                    {
                        if (item == this.currentItem)
                        {
                            using (SolidBrush brush = new SolidBrush(c2))
                            {
                                gr.FillRectangle(brush, coords[item][0], coords[item][1], coords[item][2] - coords[item][0], coords[item][3] - coords[item][1]);
                            }
                        }
                        else
                        {
                            using (SolidBrush brush = new SolidBrush(c1))
                            {
                                gr.FillRectangle(brush, coords[item][0], coords[item][1], coords[item][2] - coords[item][0], coords[item][3] - coords[item][1]);
                            }
                        }
                    }
                }
            }
        }
        /// 
        /// Handle mouse click.
        /// 
        /// Sending object.
        /// Event data.
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            Point p1 = e.Location;
            this.selectedItem = null;
            for (int i = 0; i < coords.Length; i++)
            {
                if (coords[i][0] < p1.X
                    && coords[i][2] > p1.X
                    && coords[i][1] < p1.Y
                    && coords[i][3] > p1.Y)
                {
                    bool available = true;
                    if (this.usedItems != null)
                    {
                        for (int j = 0; available && j < this.usedItems.Length; j++)
                        {
                            if (this.usedItems[j] == i && i != this.currentItem)
                            {
                                available = false;
                            }
                        }
                    }
                    if (available)
                    {
                        this.selectedItem = (uint)i;
                        break;
                    }
                }
            }
            this.textBox1.Text = this.selectedItem.ToString();
            this.okButton.Enabled = (this.selectedItem >= 0);
        }
        /// 
        /// Handle OK button click.
        /// 
        /// Sending object.
        /// Event data.
        private void okButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        /// 
        /// Handle Cancel button click.
        /// 
        /// Sending object.
        /// Event data.
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}