// 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SPT.PTZ.Core;

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

        private void PresetItemForm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Cameras.Length; i++)
            {
                _cameraCombo.Items.Add(Cameras[i].Name);
            }

            _cameraCombo.SelectedIndex = Item.Camera;

            if (Item.Span.Value == Logic.TourItem.DefaultSpanValue)
            {
                _useDefaultCheckBox.Checked = true;
            }
            else
            {
                _timeSpanEdit.Span = Item.Span;
                _timeSpanEdit.ApplyValue();
            }
        }

        private void _useDefaultCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            _timeSpanEdit.Enabled = !_useDefaultCheckBox.Checked;
        }

        private Logic.TourItem _item;

        public Logic.TourItem Item
        {
            get { return _item; }
            set { _item = value; }
        }

        private Logic.CameraItemList _cameras;

        public Logic.CameraItemList Cameras
        {
            get { return _cameras; }
            set { _cameras = value; }
        }

        private Controller _controller;

        public Controller Controller
        {
            get { return _controller; }
            set { _controller = value; }
        }
	

        private void _cameraCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sel = _cameraCombo.SelectedIndex;
            int tmp = _presetCombo.SelectedIndex;

            _presetCombo.Items.Clear();

            for (int i = 0; i < Cameras[sel].Presets.Length; i++)
            {
                _presetCombo.Items.Add(Cameras[sel].Presets[i].Name);
            }

            if (tmp >= 0)
            {
                _presetCombo.SelectedIndex = tmp;
            }
            else // 1st time
            {
                _presetCombo.SelectedIndex = Item.Preset;
            }
        }

        private void _okButton_Click(object sender, EventArgs e)
        {
            Item.Camera = _cameraCombo.SelectedIndex;
            Item.Preset = _presetCombo.SelectedIndex;

            if (_useDefaultCheckBox.Checked)
            {
                Item.Span.Value = Logic.TourItem.DefaultSpanValue;                
            }
            else
            {
                Item.Span = _timeSpanEdit.Span;
            }
        }

        private void _goToPresetButton_Click(object sender, EventArgs e)
        {
            int camera = _cameraCombo.SelectedIndex;
            int preset = _presetCombo.SelectedIndex;

            int oldCam = Controller.Address;

            Controller.Address = (byte)(camera);
            Controller.SendCommand(SPT.PTZ.Core.PtzFunction.SelectCamera,
                camera);
            Controller.SendCommand(SPT.PTZ.Core.PtzFunction.GotoPreset,
                preset + 1);
            Controller.Address = (byte)(oldCam);
            Controller.SendCommand(SPT.PTZ.Core.PtzFunction.SelectCamera,
                oldCam);
        }
    }
}