using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PicTray
{

    public class cLibPicBoxViewer : System.Windows.Forms.Form
    {
        const int conBorderSize = 50;
        const int conMapMoveStep = 5;
        Point udrMapCenter = new Point();
        public PictureBox picTerritoryMap = new PictureBox();
        public Timer tmrMoveMap = new Timer();
        public Form thisForm;

        public cLibPicBoxViewer(Image thisImage, string thisText)
        {
            thisForm = new Form();
            thisForm.Visible = true;
            thisForm.Text = thisText;
            thisForm.Controls.Add(picTerritoryMap);

            picTerritoryMap.SizeMode = PictureBoxSizeMode.AutoSize;
            picTerritoryMap.Image = thisImage;
            picTerritoryMap.Visible = true;
            udrMapCenter.X = picTerritoryMap.Width / 2; udrMapCenter.Y = picTerritoryMap.Height / 2;
            placeMap();
            picTerritoryMap.MouseMove += new MouseEventHandler(picTerritoryMap_MouseMove);
            thisForm.SizeChanged += new EventHandler(thisForm_SizeChanged);
            tmrMoveMap.Tick += new EventHandler(tmrMoveMap_Tick);
        }

        void thisForm_SizeChanged(object sender, EventArgs e) { placeMap(); }

        public void resizeToFitImage()
        {
            thisForm.Width = picTerritoryMap.Width;
            thisForm.Height = picTerritoryMap.Height + 35;
            picTerritoryMap.Left = 0; picTerritoryMap.Top = 0;
        }

        void tmrMoveMap_Tick(object sender, EventArgs e)
        {
            tmrMoveMap.Enabled = false;
            placeMap();
        }

        void placeMap()
        {
            if (udrMapCenter.X > picTerritoryMap.Width - thisForm.Width / 2)
                udrMapCenter.X = picTerritoryMap.Width - thisForm.Width / 2;
            if (udrMapCenter.X < thisForm.Width / 2)
                udrMapCenter.X = thisForm.Width / 2;
            if (udrMapCenter.Y > picTerritoryMap.Height - thisForm.Height / 2)
                udrMapCenter.Y = picTerritoryMap.Height - thisForm.Height / 2;
            if (udrMapCenter.Y < thisForm.Height / 2)
                udrMapCenter.Y = thisForm.Height / 2;

            picTerritoryMap.Left = -udrMapCenter.X + thisForm.Width / 2;
            picTerritoryMap.Top = -udrMapCenter.Y + thisForm.Height / 2;
        }

        void picTerritoryMap_MouseMove(object sender, MouseEventArgs e)
        {
            Point udrMouse = new Point();
            udrMouse.X = MousePosition.X - thisForm.Left - 5;
            udrMouse.Y = MousePosition.Y - thisForm.Top - 10;

            bool bolPlaceMap = false;

            if (udrMouse.Y < conBorderSize
                && udrMapCenter.Y > 0)
            {
                udrMapCenter.Y -= conMapMoveStep;
                bolPlaceMap = true;
            }
            else if (udrMouse.Y > thisForm.Height - conBorderSize
                && udrMapCenter.Y < picTerritoryMap.Width)
            {
                udrMapCenter.Y += conMapMoveStep;
                bolPlaceMap = true;
            }

            if (udrMouse.X < conBorderSize
                && udrMapCenter.X > 0)
            {
                udrMapCenter.X -= conMapMoveStep;
                bolPlaceMap = true;
            }
            else if (udrMouse.X > thisForm.Width - conBorderSize
                && udrMapCenter.X < picTerritoryMap.Width)
            {
                udrMapCenter.X += conMapMoveStep;
                bolPlaceMap = true;
            }

            if (bolPlaceMap)
                tmrMoveMap.Enabled = true;
        }
    }
}