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

namespace PTZController
{
    public partial class AutoScanForm : PanelForm
    {
        public AutoScanForm()
        {
            InitializeComponent();
        }

        private void AutoScanForm_Load(object sender, EventArgs e)
        {

            UpdateSpeed();
            UpdateButtons();

            clockWiseButton.PressedFlag = ClockWise;
            counterClockWiseButton.PressedFlag = !ClockWise;
        }

        /// <summary>
        /// Checks if operation location is valid.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private void MoveSpeedBar(Point pt)
        {
            Rectangle rc = new Rectangle(sliderBar.Left, sliderButton.Top,
                sliderBar.Width, sliderButton.Height);

            if (rc.Contains(pt))
            {
                int pos = pt.X;
                int range = Controller.MaxScanSpeed - Controller.MinScanSpeed;
                int width = sliderBar.Width - sliderButton.Width;

                Speed = Controller.MinScanSpeed + (pos - sliderBar.Left) * range / width;
                UpdateSpeed();
            }
        }

        private void UpdateSpeed()
        {
            int width = sliderBar.Width - sliderButton.Width;
            int range = Controller.MaxScanSpeed - Controller.MinScanSpeed;

            speedLabel.Text = Speed.ToString();
            sliderButton.Left = sliderBar.Left + (Speed - Controller.MinScanSpeed) * width / range;
        }

        private void UpdateDirection()
        {
            if (clockWiseButton.PressedFlag)
            {
                ClockWise = true;
            }
            else
            {
                ClockWise = false;
            }
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            m_started = true;

            UpdateDirection();
            UpdateButtons();

            _controller.AutoScan(m_started, ClockWise, Speed);
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            m_started = false;

            UpdateButtons();

            _controller.AutoScan(m_started, ClockWise, Speed);
        }

        private void UpdateButtons()
        {
            clockWiseButton.Enabled = !m_started;
            clockWiseButton.PressedFlag = ClockWise;
            counterClockWiseButton.Enabled = !m_started;
            counterClockWiseButton.PressedFlag = !ClockWise;
            startButton.Enabled = !m_started;
            stopButton.Enabled = m_started;
            if (!clockWiseButton.Enabled)
            {
                if (clockWiseButton.PressedFlag)
                {
                    clockWiseButton.Image = Properties.Resources.btn_clockwise_select_disable;
                }
            }

            if (!counterClockWiseButton.Enabled)
            {
                if (counterClockWiseButton.PressedFlag)
                {
                    counterClockWiseButton.Image = Properties.Resources.btn_counterclockwise_select_disable;
                }
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            if (m_started)
            {
                m_started = false;
                _controller.AutoScan(m_started, ClockWise, Speed);
            }

            Close();
        }

        protected override void HandleClose()
        {
            Close();
        }

        private void AutoScanForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (m_started)
            {
                m_started = false;
                _controller.AutoScan(m_started, ClockWise, Speed);
            }

            UpdateDirection();
        }

        private void sliderBar_MouseClick(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);

            pt = PointToClient(sliderBar.PointToScreen(pt));

            MoveSpeedBar(pt);
        }

        private void sliderButton_MouseDown(object sender, MouseEventArgs e)
        {
            m_mouseDown = true;
        }

        private void sliderButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_mouseDown)
            {
                Point pt = PointToClient(sliderButton.PointToScreen(new Point(e.X, e.Y)));

                MoveSpeedBar(pt);
            }
        }

        private void sliderButton_MouseUp(object sender, MouseEventArgs e)
        {
            m_mouseDown = false;
        }

        private void clockWiseButton_Click(object sender, EventArgs e)
        {
            counterClockWiseButton.PressedFlag = !counterClockWiseButton.PressedFlag;
        }

        private void counterClockWiseButton_Click(object sender, EventArgs e)
        {
            clockWiseButton.PressedFlag = !clockWiseButton.PressedFlag;
        }

        private bool m_started = false;

        private int m_speed = Controller.MinScanSpeed;
        
        public int Speed
        {
            get { return m_speed; }
            set { m_speed = value; }
        }

        private bool m_clockWise = true;

        public bool ClockWise
        {
            get { return m_clockWise; }
            set { m_clockWise = value; }
        }
	
        private bool m_mouseDown = false;

        private Controller _controller;

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