// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2009.
// All software Source Code, Images, Database-Design and code, Graphics Design 
// and source files, and related content (collectively referred to as SOURCE) are 
// Copyright (c) SPT, 2009, All Rights Reserved.
// http://SerialPortTool.com

// PTZ Controller is a Registered Trademark of SerialPortTool.com.


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

namespace PTZController
{
    public partial class ItemForm : Form
    {
        public ItemForm()
        {
            InitializeComponent();
        }

        private CustomItem m_item;

        public CustomItem Item
        {
            get { return m_item; }
            set 
            {
                // copy one
                m_item = new CustomItem(value);
            }
        }

        private CameraItemList m_cameras;

        public CameraItemList Cameras
        {
            get { return m_cameras; }
            set { m_cameras = value; }
        }

        private void ItemForm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < m_cameras.Length; i++)
            {
                cameraCombo.Items.Add(m_cameras[i].ToString());
            }

            nameEdit.Text = Item.Name;

            if (Item.Command == CustomItem.CustomCommand.Function)
	        {
                functionRadio.Checked = true;
                hexDataRadio.Checked = false;
	        }
            else 
            {
                functionRadio.Checked = false;
                hexDataRadio.Checked = true;
            }

            cameraCombo.SelectedIndex = Item.Camera - 1;
            typeCombo.SelectedIndex = (int)Item.Mode;
            // valueCombo.SelectedIndex = Item.Value - 1;
            hexDataEdit.Text = Item.HexString;

            _descriptionEdit.Text = Item.Description;

            UpdateControls();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            Item.Name = nameEdit.Text;
            Item.Description = _descriptionEdit.Text;

            if (functionRadio.Checked)
            {
                Item.Command = CustomItem.CustomCommand.Function;

                Item.Camera = cameraCombo.SelectedIndex + 1;
                Item.Mode = (CustomItem.FunctionMode) typeCombo.SelectedIndex;
                Item.Value = int.Parse(valueCombo.Text);

                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                byte[] hex = null;

                try
                {
                    hex = HelpFunction.GetHexFromString(hexDataEdit.Text);

                    Item.Command = CustomItem.CustomCommand.HexData;
                    Item.HexValue = hex;

                    DialogResult = DialogResult.OK;
                    Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Invalid hex string input. ", Text, MessageBoxButtons.OK);
                    hexDataEdit.Focus();
                    hexDataEdit.SelectAll();
                }
            }
        }

        private void functionRadio_CheckedChanged(object sender, EventArgs e)
        {
            UpdateControls();
        }

        private void typeCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            int n = Item.Value - 1;

            if (typeCombo.SelectedIndex == PresetIndex)
            {
                valueCombo.Items.Clear();

                for (int i = 0; i < PtzConfigure.PresetCount; i++)
                {
                    valueCombo.Items.Add((i + 1).ToString());
                }

                if (n < valueCombo.Items.Count)
                {
                    valueCombo.SelectedIndex = n;
                }
                else
                {
                    valueCombo.SelectedIndex = 0;
                }
            }
            else
            {
                valueCombo.Items.Clear();

                for (int i = 0; i < PtzConfigure.AuxCount; i++)
                {
                    valueCombo.Items.Add((i + 1).ToString());
                }

                if (n < valueCombo.Items.Count)
                {
                    valueCombo.SelectedIndex = n;
                }
                else
                {
                    valueCombo.SelectedIndex = 0;
                }
            }
        }


        private void valueCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Item.Value = int.Parse(valueCombo.Text);
        }

        private const int PresetIndex = 2;

        private void UpdateControls()
        {
            cameraCombo.Enabled = functionRadio.Checked;
            typeCombo.Enabled = functionRadio.Checked;
            valueCombo.Enabled = functionRadio.Checked;
            hexDataEdit.Enabled = !functionRadio.Checked;
        }
 
        private void hexDataRadio_CheckedChanged(object sender, EventArgs e)
        {
            functionRadio.Checked = !hexDataRadio.Checked;
        }
    }
}