using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Globalization;
using System.IO;
using System.Collections;

namespace iTripLog.Map.Controler
{
    public class ControlButton : Control
    {
       
        Image backgroundImage;
        bool pressed = false;
        int type = 0;
        MapBGFrom frame;
        bool changed=false;

        public ControlButton(Image e,int type,MapBGFrom frame)
        {
            backgroundImage = e;
            this.type = type;
            this.frame = frame;
        }
       
        // Property for the background image to be drawn behind the button text.
        public Image BackgroundImage
        {
            get
            {
                return this.backgroundImage;
            }
            set
            {
                this.backgroundImage = value;
            }
        }

        // When the mouse button is pressed, set the "pressed" flag to true 
        // and invalidate the form to cause a repaint.  The .NET Compact Framework 
        // sets the mouse capture automatically.
        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.pressed = false;
            this.BackColor = Color.Blue;
            base.OnMouseUp(e);
        }



        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.pressed = true;
            this.BringToFront();
            switch (type)
            {
                case 1:
                    {//up button 
                        frame.latitude = frame.latitude + 1;
                        changed = true;
                        goto end;
                    }
                case 2:
                    {//down button
                        frame.latitude = frame.latitude + 1;
                        changed = true;
                        goto end;
                    }
                case 3:
                    {//left button
                        frame.longitude = frame.longitude - 1;
                        changed = true;
                        goto end;
                    }
                case 4:
                    {//right button
                        frame.longitude = frame.longitude + 1;
                        changed = true;
                        goto end;
                    }
                case 5:
                    {//zoom level small button
                        if (frame.zoomLevel >= 1)
                        {
                            frame.zoomLevel = frame.zoomLevel - 1;
                            changed = true;
                        }
                        goto end;
                    }
                case 6:
                    {//zoom level big button
                        if (frame.zoomLevel < 17)
                        {
                            frame.zoomLevel = frame.zoomLevel + 1;
                            changed = true;
                        }
                        goto end;
                    }
                case 7:
                    {
                        changed = true;
                        // view all the picture in ppt
                        goto end;
                    }
                case 8:
                    {
                        changed = true;
                        //outside
                        goto end;
                    }

            }
            end:
            if (changed)
            {
                if (type <= 6 &&type >= 1)
                {
                    frame.ChangeMap(type);
                }
                else if (type == 8)
                {
                    frame.ViewPictures();
                }
                else if (type == 7)
                {
                    frame.Exit();
                }
            }
            this.BackColor = Color.White;
            changed = false;
            base.OnMouseDown(e);
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.ActiveCaption), e.ClipRectangle);

            if (this.backgroundImage != null)
            {
                System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
                attr.SetColorKey(Color.Magenta, Color.Magenta);

                if (this.pressed)
                {
                    //this.BackColor = Color.Black;
                    e.Graphics.DrawImage(new Bitmap("Storage Card\\picture\\icon_00"+type+".png"), this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);
                }
                else
                {
                    //this.BackColor = Color.White;
                    e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);
                }
            }

            base.OnPaint(e);
        }
    }
}
