// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2007.
// 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, 2007, 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 System.Runtime.InteropServices;
using SPT.PTZ.Core;

namespace HelloPTZ
{
    public partial class frmMain : Form
    {
        /// <summary>
        /// PTZ Controller object
        /// </summary>
        Controller _controller;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            String[] portNames = Controller.PortNames;

            for (int i = 0; i < portNames.Length; i++)
            {
                _COMPortCombo.Items.Add(portNames[i]);
            }

            if (portNames.Length > 0) // select 1st com port
            {
                _COMPortCombo.SelectedIndex = 0;
            }

            _baudrateCombo.SelectedIndex = 2;

            _protocolsCombo.Items.Add(Controller.PelcoD);
            _protocolsCombo.Items.Add(Controller.PelcoP);

            _protocolsCombo.SelectedIndex = 0;

            for (int i = 0; i < 256 - 1; i++)
            {
                _addressCombo.Items.Add((i + 1).ToString());
            }

            _addressCombo.SelectedIndex = 0;
        }

        private void _COMPortButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (_controller != null) // close previous port
                {
                    _controller.ClosePort();
                }

                _controller = new Controller();
                _controller.OpenPort(_COMPortCombo.Text, int.Parse(_baudrateCombo.Text));
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                _controller = null;
            }
        }

        private void _zoomInButton_MouseDown(object sender, MouseEventArgs e)
        {
            DoSimpleCommand(PtzFunction.ZoomIn);
        }

        /// <summary>
        /// Sends simple command to PTZ
        /// </summary>
        /// <param name="function">function of the command</param>
        private void DoSimpleCommand(PtzFunction function)
        {
            if (CheckPort())
            {
                _controller.ChangeProtocol(_protocolsCombo.Text);
                _controller.Address = byte.Parse(_addressCombo.Text);
                _controller.SendCommand(function);
            }
        }

        /// <summary>
        /// Checks if user opens port
        /// </summary>
        /// <returns>true if opened, false if not</returns>
        private bool CheckPort()
        {
            bool flag = true;

            if (_controller == null)
            {
                MessageBox.Show("Please open port first.");
                flag = false;
            }

            return flag;
        }

        private void _zoomOutButton_MouseDown(object sender, MouseEventArgs e)
        {
            DoSimpleCommand(PtzFunction.ZoomOut);
        }

        private void StopAction(object sender, MouseEventArgs e)
        {
            DoSimpleCommand(PtzFunction.StopAction);
        }
    }
}