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

namespace PostCardHitsuji.Ui.Controls
{
    public partial class LayoutBaseUserControl : UserControl
    {
        public event EventHandler OnLabelItemSelectedEvent;
        public event EventHandler OnIllustItemSelectedEvent;

        public List<PostCardLabelItemUnit> labelList;
        public List<PostCardIllustItemUnit> illustList;


        public new int Width
        {
            get { return this.Size.Width; }
            set
            {
                if (value == this.Size.Width) return;
                this.Size = new Size(value, this.Size.Height);
                adjustLocation();
            }
        }
        public new int Height
        {
            get { return this.Size.Height; }
            set
            {
                if (value == this.Size.Height) return;
                this.Size = new Size(this.Size.Width, value);
                adjustLocation();
            }
        }

        public int InnerWidth
        {
            get { return postCardLayoutRectangleClass.RectWidth; }
            set
            {
                if (value == postCardLayoutRectangleClass.RectWidth) return;
                postCardLayoutRectangleClass.RectWidth = value;
                adjustLocation();
            }
        }
        public int InnerHeight
        {
            get { return postCardLayoutRectangleClass.RectHeight; }
            set
            {
                if (value == postCardLayoutRectangleClass.RectHeight) return;
                postCardLayoutRectangleClass.RectHeight = value;
                adjustLocation();
            }
        }

        public string layoutType = "V";
        public readonly string layoutTypeV = "V"; // V/H (縦長：V、横長：H)
        public readonly string layoutTypeH = "H"; // V/H (縦長：V、横長：H)
        private readonly int widthAsTypeV = 300;
        private readonly int heightAsTypeV = 450;
        private readonly int widthAsTypeH = 450;
        private readonly int heightAsTypeH = 300;
        private int mouseDownLocationX = 0;
        private int mouseDownLocationY = 0;
        protected PictureBox pictureBox { get { return this.pictureBox1; } }

        protected PostCardLabelItemUnit postCardLayoutRectangleClass;
        
        public LayoutBaseUserControl()
        {
            InitializeComponent();

            this.labelList = new List<PostCardLabelItemUnit>();
            this.illustList = new List<PostCardIllustItemUnit>();

        }

        private void adjustLocation()
        {
            if (Height < InnerHeight)
            {
                postCardLayoutRectangleClass.DefaultLocationY = 0;
                postCardLayoutRectangleClass.CurrentLocationY = 0;
            }
            else
            {
                postCardLayoutRectangleClass.DefaultLocationY = (Height - InnerHeight) / 2;
                postCardLayoutRectangleClass.CurrentLocationY = (Height - InnerHeight) / 2;
            }
            if (Width < InnerWidth)
            {
                postCardLayoutRectangleClass.CurrentLocationX = 0;
                postCardLayoutRectangleClass.CurrentLocationX = 0;
            }
            else
            {
                postCardLayoutRectangleClass.CurrentLocationX = (Width - InnerWidth) / 2;
                postCardLayoutRectangleClass.CurrentLocationX = (Width - InnerWidth) / 2;
            }
        }

        private void PostCardLayoutUserControl_Load(object sender, EventArgs e)
        {
            postCardLayoutRectangleClass = new PostCardLabelItemUnit("PostCardLayoutRectangleClass");
            postCardLayoutRectangleClass.InnerSolidBrush = new SolidBrush(Color.WhiteSmoke);
            postCardLayoutRectangleClass.RectWidth = widthAsTypeV;
            postCardLayoutRectangleClass.RectHeight = heightAsTypeV;
            postCardLayoutRectangleClass.CurrentLocationX = (this.Width - widthAsTypeV) / 2;
            postCardLayoutRectangleClass.CurrentLocationY = (this.Height - heightAsTypeV) / 2;

            this.pictureBox1.Width = Width;
            this.pictureBox1.Height = Height;
            this.pictureBox1.Location = new Point(0, 0);
            
        }

        public void setPostCardLayoutType(string type_VorH)
        {
            if (type_VorH.Equals(layoutTypeV))
            {
                postCardLayoutRectangleClass.RectWidth = widthAsTypeV;
                postCardLayoutRectangleClass.RectHeight = heightAsTypeV;
                postCardLayoutRectangleClass.CurrentLocationX = (Width - widthAsTypeV) / 2;
                postCardLayoutRectangleClass.CurrentLocationY = (Height - heightAsTypeV) / 2;

            }
            else if (type_VorH.Equals(layoutTypeH))
            {
                postCardLayoutRectangleClass.RectWidth = widthAsTypeH;
                postCardLayoutRectangleClass.RectHeight = heightAsTypeH;
                postCardLayoutRectangleClass.CurrentLocationX = (Width - widthAsTypeH) / 2;
                postCardLayoutRectangleClass.CurrentLocationY = (Height - heightAsTypeH) / 2;
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //台紙のRectangleを描画
            postCardLayoutRectangleClass.drawInnerRect(e);

            foreach(var illustItem in this.illustList)
            {
                illustItem.drawUnionRect(e);
            }

            foreach(var labelItem in this.labelList)
            {
                labelItem.drawUnionRect(e);
            }
            //this.Refresh();
        }

        private void PostCardLayoutUserControl_MouseDown(object sender, MouseEventArgs e)
        {
            this.mouseDownLocationX = e.X;
            this.mouseDownLocationY = e.Y;

            //開放
            foreach (PostCardItem labelItem in this.labelList) labelItem.releaseAbsolute();
            foreach (PostCardItem illustItem in this.illustList) illustItem.releaseAbsolute();

            //ポイントフラグを反転
            foreach (PostCardItem labelItem in this.labelList)
            {
                if (labelItem.isContain(e.X, e.Y)) labelItem.startPoint();
                
            }
            foreach (PostCardItem illustItem in this.illustList)
            {
                if(illustItem.isContain(e.X, e.Y)) illustItem.startPoint();
            }

            if (this.OnLabelItemSelectedEvent != null &&
                this.getPointedLabelItems().Count > 0)
            {
                this.OnLabelItemSelectedEvent(this, e);
            }
            if(this.OnIllustItemSelectedEvent != null &&
               this.getPointedIllustItems().Count > 0)
            {
                this.OnIllustItemSelectedEvent(this, e);
            }
        }

        private void PostCardLayoutUserControl_MouseUp(object sender, MouseEventArgs e)
        {
            foreach (PostCardItem labelItem in this.labelList) labelItem.endPoint();
            foreach (PostCardItem illustItem in this.illustList) illustItem.endPoint();
            this.pictureBox1.Refresh();
        }

        private void PostCardLayoutUserControl_MouseMove(object sender, MouseEventArgs e)
        {
            foreach (PostCardLabelItemUnit labelItem in this.labelList)
            {
                if (labelItem.IsPointing)
                {
                    labelItem.movePoint(e.X - this.mouseDownLocationX, 
                                        e.Y - this.mouseDownLocationY);
                    this.pictureBox1.Refresh();
                    return;
                }
            }
            foreach (PostCardIllustItemUnit illustItem in this.illustList)
            {
                if (illustItem.IsPointing)
                {
                    illustItem.movePoint(e.X - this.mouseDownLocationX, 
                                         e.Y - this.mouseDownLocationY);

                    this.pictureBox1.Refresh();
                    return;
                }
            }
        }

        #region マウスポイントのフラグ設定

        protected void pointIllustItems(int x, int y)
        {
            //List<PostCardLabelItemUnit> pointingList = new List<PostCardLabelItemUnit>();
            foreach (var item in this.illustList)
            {
                if (item.isContain(x, y)) (item as PostCardIllustItemUnit).startPoint();
                //{
                //    item.IsPointing = true;
                //    item.wasPointed = true;
                //    item.defaultRectLocationX = item.RectLocationX;
                //    item.defaultRectLocationY = item.RectLocationY;

                //}
            }
        }

        #endregion

        #region ポイントされているアイテム取得

        public List<PostCardLabelItemUnit> getPointedLabelItems()
        {
            List<PostCardLabelItemUnit> pointingList = new List<PostCardLabelItemUnit>();
            foreach (var item in this.labelList)
            {
                if (item.IsPointing)
                {
                    pointingList.Add(item);
                }
            }
            return pointingList;
        }

        public List<PostCardIllustItemUnit> getPointedIllustItems()
        {
            List<PostCardIllustItemUnit> pointingList = new List<PostCardIllustItemUnit>();
            foreach (var item in this.illustList)
            {
                if (item.IsPointing)
                {
                    pointingList.Add(item);
                }
            }
            return pointingList;
        }

        #endregion


        protected List<PostCardLabelItemUnit> getPointingLabelItems(int x, int y)
        {
            List<PostCardLabelItemUnit> hitList = new List<PostCardLabelItemUnit>();
            foreach (var item in this.labelList)
            {
                if (item.isContain(x, y))
                {
                    hitList.Add(item);
                }
            }
            return hitList;
        }

        protected PostCardLabelItemUnit getHitInnerRectClassPrim(int locationX, int locationY)
        {
            foreach (PostCardLabelItemUnit rectClass in this.labelList)
            {
                if (rectClass.isContain(locationX, locationY))
                {
                    return rectClass;
                }
            }
            return null;
        }

    }

    interface IPostCardLayoutItem
    {
        string getName();

    }

    public class PostCardItem : IPostCardLayoutItem
    {
        
        protected Rectangle InnerRect;
        protected Rectangle OuterRect;

        public bool IsPointing 
        {
            get;
            private set; 
        }
        public bool WasPointed 
        { 
            get;
            private set;
        }


        public string ClassName
        {
            get; protected set;
        }

        public string getName()
        {
            return this.ClassName;
        }

        #region Inner, Outer の Rectangle の位置

        private int currentLocationX;
        public int CurrentLocationX
        {
            get { return currentLocationX; }
            set
            {
               currentLocationX = value;
               this.InnerRect.X = value;
               this.OuterRect.X = value;
            }
        }
        private int currentLocationY;
        public int CurrentLocationY
        {
            get { return currentLocationY; }
            set
            {
                currentLocationY = value;
                this.InnerRect.Y = value;
                this.OuterRect.Y = value;
            }
        }

        #endregion

        #region 移動初期位置

        private int defaultLocationX;
        public int DefaultLocationX
        {
            get { return defaultLocationX; }
            set 
            {
                defaultLocationX = value;
            }
        }

        private int defaultLocationY;
        public int DefaultLocationY
        {
            get { return defaultLocationY; }
            set
            {
                defaultLocationY = value;
            }
        }

        #endregion

        #region Inner, Outer の Rectangle のサイズ

        public int RectWidth
        {
            get { return this.InnerRect.Width; }
            set
            {
                this.InnerRect.Width = value;
                this.OuterRect.Width = value;
            }
        }
        public int RectHeight
        {
            get { return this.InnerRect.Height; }
            set
            {
                this.InnerRect.Height = value;
                this.OuterRect.Height = value;
            }
        }

        #endregion

        public bool isContain(int locationX, int locationY)
        {
            if (this.OuterRect.Contains(locationX, locationY))
            {
                return true;
            }
            return false;
        }

        public void startPoint()
        {
            IsPointing = true;
            WasPointed = true;
            this.DefaultLocationX = this.CurrentLocationX;
            this.DefaultLocationY = this.CurrentLocationY;
        }

        public void movePoint(int moveX, int moveY)
        {
            this.CurrentLocationX = this.defaultLocationX + moveX;
            this.CurrentLocationY = this.defaultLocationY + moveY;
        }

        public void endPoint()
        {
            this.IsPointing = false;
        }

        public void releaseAbsolute()
        {
            this.IsPointing = false;
            this.WasPointed = false;
        }

    }

    /// <summary>
    /// ラベルアイテムの状態と振る舞いを持つクラス
    /// </summary>
    public class PostCardLabelItemUnit : PostCardItem
    {
        //public string ClassName { get; private set; }
        public string AllowReSizeStatus;
        public SolidBrush InnerSolidBrush;
        public Pen OuterPen;
        
        #region FontSize

        private int fontSize = 10;
        public int FontSize
        {
            get { return fontSize; }
            set
            { 
                fontSize = value;
                this.messageFont = new Font(this.fontFamilyName, this.fontSize);
            }
        }

        #endregion

        #region FontColor

        private Color fontColor = Color.Black;
        public Color FontColor
        {
            get { return fontColor; }
            set
            {
                fontColor = value;
                messageBrush = new SolidBrush(fontColor);
            }
        }

        #endregion

        #region FontFamilyName

        private string fontFamilyName;
        public string FontFamilyName 
        {
            get { return fontFamilyName; }
            set
            { 
                fontFamilyName = value;
                this.messageFont = new Font(this.fontFamilyName, this.fontSize);
            }
        }

        #endregion

        #region MessageVector

        private string messageVector = "H";
        public string MessageVector
        {
            get { return messageVector; }
            set 
            { 
                messageVector = value;
                this.messageStrFormat.FormatFlags 
                    = (messageVector.Equals(PostCardLabelItemUnit.MSGVECTOR_H))  
                        ? StringFormatFlags.LineLimit : StringFormatFlags.DirectionVertical;
            }
        }

        #endregion

        #region Message

        //private string message;
        //public string Message
        //{
        //    get { return message; }
        //    set 
        //    { 
        //        message = value;
        //        this.setMessageList();
        //    }
        //}
        
        private string message;
        public string Message
        {
            get { return message; }
            set
            {
                MessageList.Clear();
                if(string.IsNullOrEmpty(value))
                {
                    message = "";
                }
                else
                {
                    message = value;
                    StringBuilder line = new StringBuilder("");
                    for (int index = 0; index < message.Length; index++)
                    {
                        if (message.Substring(index, 1).Equals("\n"))
                        {
                            MessageList.Add(line.ToString());
                            line = new StringBuilder("");
                        }
                        else
                        {
                            line.Append(message.Substring(index, 1));
                        }
                    }
                    MessageList.Add(line.ToString());
                }
            }
        }
        #endregion

        public List<string> MessageList = new List<string>();
        public StringFormatFlags MsgFormat = StringFormatFlags.LineLimit;
        public int MessageFontSize = 10;

        public static readonly string RESIZEABLE_NO = "RESIZEABLE_NO";
        public static readonly string RESIZEABLE_V = "RESIZEABLE_V";
        public static readonly string RESIZEABLE_H = "RESIZEABLE_H";
        public static readonly string RESIZEABLE_BOTH = "RESIZEABLE_BOTH";

        public static readonly string MSGVECTOR_V = "V";
        public static readonly string MSGVECTOR_H = "H";


        public PostCardLabelItemUnit(string className)
        {
            ClassName = className;
            this.OuterRect = new Rectangle();
            this.InnerRect = new Rectangle();
            //this.IsPointing = false;
            this.AllowReSizeStatus = RESIZEABLE_NO;
            this.InnerSolidBrush = new SolidBrush(Color.LightBlue);
            this.OuterPen = new Pen(Brushes.LightGreen);
            this.releaseAbsolute();
        }

        #region 描画

        public void drawInnerRect(PaintEventArgs ev)
        {
            ev.Graphics.FillRectangle(this.InnerSolidBrush, this.InnerRect);
            drawDirectReSizeRect(ev);
            ev.Graphics.DrawString(this.Message, 
                                   this.messageFont, 
                                   this.messageBrush,
               new Rectangle(this.CurrentLocationX,
                             this.CurrentLocationY,
                             400,
                             400),
               messageStrFormat);
        }

        public void drawOuterRect(PaintEventArgs ev)
        {
            ev.Graphics.DrawRectangle(this.OuterPen, this.OuterRect); 
            drawDirectReSizeRect(ev);
            ev.Graphics.DrawString(this.Message, 
                                   this.messageFont, 
                                   this.messageBrush,
               new Rectangle(this.CurrentLocationX,
                             this.CurrentLocationY,
                             400,
                             400),
               messageStrFormat);

        }

        int isNextLine = 0;
        private SolidBrush messageBrush = new SolidBrush(Color.Blue);
        private Font messageFont = new Font("normal", 10);
        private StringFormat messageStrFormat = new StringFormat(StringFormatFlags.LineLimit);
        public void drawUnionRect(PaintEventArgs ev)
        {
            ev.Graphics.FillRectangle(this.InnerSolidBrush, this.InnerRect);
            drawDirectReSizeRect(ev);
            ev.Graphics.DrawRectangle(this.OuterPen, this.OuterRect);
            
            if (string.IsNullOrEmpty(this.messageVector) || 
                string.IsNullOrEmpty(Message)) return;

            messageBrush.Color = fontColor;
            if (this.MessageVector.Equals(PostCardLabelItemUnit.MSGVECTOR_V))
            {
                isNextLine = 0;
                int leng = System.Windows.Forms.TextRenderer.MeasureText(MessageList[0].ToString(), this.messageFont).Height;
                foreach (string line in MessageList)
                {
                    ev.Graphics.DrawString(line,
                                                       messageFont, this.messageBrush,
                                                        new Rectangle(this.CurrentLocationX + -leng * isNextLine,
                                                                      this.CurrentLocationY,
                                                                      400,
                                                                      400),
                                                        new StringFormat(messageStrFormat));
                    isNextLine++;
                }
                isNextLine = 0;
            }
            else
            {
                ev.Graphics.DrawString(this.Message, messageFont, this.messageBrush,
                                                    new Rectangle(this.CurrentLocationX,
                                                                  this.CurrentLocationY,
                                                                  400,
                                                                  400),
                                                    new StringFormat(messageStrFormat));
            }
            
        }

        private void drawDirectReSizeRect(PaintEventArgs ev)
        {
            Brush brush = null;
            Point[] points = null;
            if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_NO))
            {
                return;
            }
            else if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_V))
            {
                brush = Brushes.Blue;
                points = new Point[] {new Point(InnerRect.X + (InnerRect.Width / 2) - 5, InnerRect.Y + InnerRect.Height - 6), 
                                      new Point(InnerRect.X + (InnerRect.Width / 2) + 5, InnerRect.Y + InnerRect.Height - 6), 
                                      new Point(InnerRect.X + (InnerRect.Width / 2), InnerRect.Y + InnerRect.Height - 1) };
            }
            else if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_H))
            {
                brush = Brushes.Blue;
                points = new Point[] {new Point(InnerRect.X + InnerRect.Width - 6, InnerRect.Y + (InnerRect.Height / 2) - 5), 
                                      new Point(InnerRect.X + InnerRect.Width - 6, InnerRect.Y + (InnerRect.Height / 2) + 5), 
                                      new Point(InnerRect.X + InnerRect.Width - 1, InnerRect.Y + (InnerRect.Height / 2) - 1) };
            }
            else if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_BOTH))
            {
                brush = Brushes.LightGreen;
                points = new Point[] {new Point(InnerRect.X + InnerRect.Width - 12, InnerRect.Y + InnerRect.Height - 1), 
                                      new Point(InnerRect.X + InnerRect.Width - 1, InnerRect.Y + InnerRect.Height - 12), 
                                      new Point(InnerRect.X + InnerRect.Width - 1, InnerRect.Y + InnerRect.Height - 1) };
            }

            if (brush != null && points != null)
            {
                ev.Graphics.FillPolygon(brush, points);
            }

        }

        #endregion

    }

    /// <summary>
    /// イラストアイテムの状態と振る舞いを持つクラス
    /// </summary>
    public class PostCardIllustItemUnit : PostCardItem
    {
        private Image image;
        public Image Image
        {
            get { return image; }
            set
            {
                image = value;
                if (image == null) return;
                this.RectWidth = image.Width;
                this.RectHeight = image.Height;
            }
        }

        private string imageFilePath;
        public string ImageFilePath
        {
            get { return imageFilePath; }
            set
            {
                imageFilePath = value;
                this.Image = (System.IO.File.Exists(value)) ? Image.FromFile(value) : null;
                if (System.IO.File.Exists(value))
                {
                    this.image = System.Drawing.Image.FromFile(value);
                    this.RectWidth = image.Width;
                    this.RectHeight = image.Height;
                }
                else
                {
                    this.image = null;
                    this.RectWidth = 60;
                    this.RectHeight = 60;
                }
            }
        }

        public string ImageFileName
        {
            get;
            set;
        }
        
        public string AllowReSizeStatus;
        public SolidBrush InnerSolidBrush;
        public SolidBrush OuterSolidBrush;
        
        private string message;
        public string Message
        {
            get { return message; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    message = "";
                    MessageList.Clear();
                }
                else
                {
                    message = value;
                    StringBuilder line = new StringBuilder("");
                    for (int index = 0; index < message.Length - 1; index++)
                    {
                        if (message.Substring(index, 1).Equals("\n") || index + 1 == (Message.Length))
                        {
                            MessageList.Add(line.ToString());
                            line = new StringBuilder("");
                        }
                        else{
                            line.Append(message.Substring(index, 1));
                            }
                    }
                }
            }
        }
        public List<string> MessageList = new List<string>();
        public string MsgVector = "H";
        public StringFormatFlags MsgFormat = StringFormatFlags.LineLimit;
        public int MessageFontSize = 10;

        public static readonly string RESIZEABLE_NO = "RESIZEABLE_NO";
        public static readonly string RESIZEABLE_V = "RESIZEABLE_V";
        public static readonly string RESIZEABLE_H = "RESIZEABLE_H";
        public static readonly string RESIZEABLE_BOTH = "RESIZEABLE_BOTH";

        public static readonly string MSGVECTOR_V = "V";
        public static readonly string MSGVECTOR_H = "H";


        #region Inner, Outer の Rectangle のサイズ

        public int RectWidth
        {
            get { return this.InnerRect.Width; }
            set
            {
                this.InnerRect.Width = value;
                this.OuterRect.Width = value;
            }
        }
        public int RectHeight
        {
            get { return this.InnerRect.Height; }
            set
            {
                this.InnerRect.Height = value;
                this.OuterRect.Height = value;
            }
        }

        #endregion

        public PostCardIllustItemUnit(string className)
        {
            ClassName = className;
            this.OuterRect = new Rectangle();
            this.InnerRect = new Rectangle();
            //this.IsPointing = false;
            this.AllowReSizeStatus = RESIZEABLE_NO;
            this.InnerSolidBrush = new SolidBrush(Color.YellowGreen);
            this.OuterSolidBrush = new SolidBrush(Color.YellowGreen);
            
        }

        public string getName()
        {
            return ClassName;
        }

        #region 描画

        public void drawInnerRect(PaintEventArgs ev)
        {
            if (this.Image != null)
            {
                ev.Graphics.DrawImage(this.Image, CurrentLocationX, CurrentLocationY);
            }
            else
            {
                ev.Graphics.FillRectangle(this.InnerSolidBrush, CurrentLocationX, CurrentLocationY, 20, 20);
            }


            //ev.Graphics.FillRectangle(this.InnerSolidBrush, this.InnerRect);
            //drawDirectReSizeRect(ev);
            //ev.Graphics.DrawString(this.Message, new Font("normal", this.MessageFontSize), Brushes.Blue,
            //   new Rectangle(this.RectLocationX,
            //                 this.RectLocationY,
            //                 400,
            //                 400),
            //   new StringFormat(MsgFormat));
        }

        public void drawOuterRect(PaintEventArgs ev)
        {
            ev.Graphics.DrawRectangle(new Pen(OuterSolidBrush), this.OuterRect); 
            drawDirectReSizeRect(ev);
            ev.Graphics.DrawString(this.Message, new Font("normal", this.MessageFontSize), Brushes.Blue,
               new Rectangle(this.CurrentLocationX,
                             this.CurrentLocationY,
                             400,
                             400),
               new StringFormat(MsgFormat));

        }

        int isNextLine = 0;
        public void drawUnionRect(PaintEventArgs ev)
        {
            ev.Graphics.FillRectangle(this.InnerSolidBrush,
                                      this.CurrentLocationX,
                                      this.CurrentLocationY, 
                                      this.RectWidth, 
                                      this.RectHeight);
            if (this.Image != null)
            {
                ev.Graphics.DrawImage(this.Image, CurrentLocationX, CurrentLocationY);
                ev.Graphics.DrawRectangle(new Pen(OuterSolidBrush), this.OuterRect);
            }
            else
            {
                //ev.Graphics.FillRectangle(this.InnerSolidBrush, RectLocationX, RectLocationY, RectWidth, RectHeight);
                //drawDirectReSizeRect(ev);
                ev.Graphics.DrawRectangle(new Pen(OuterSolidBrush), this.OuterRect);
            }
            //ev.Graphics.FillRectangle(this.InnerSolidBrush, this.InnerRect);
            //drawDirectReSizeRect(ev);
            //ev.Graphics.DrawRectangle(new Pen(OuterSolidBrush), this.OuterRect);
            //ev.Graphics.DrawString(this.Message, new Font("normal", 10), Brushes.Blue, this.RectLocationX, RectLocationY);
            if (string.IsNullOrEmpty(Message)) return;
            if (this.MsgVector.Equals(PostCardLabelItemUnit.MSGVECTOR_V))
            {
                //StringBuilder msgBuilder = new StringBuilder("");
                ////次行のメッセージのカウント
                //isNextLine = 0;
                ////縦書き指定の場合、メッセージリストの値を描画していく
                //for (int index = 0; index < this.Message.Length; index++)
                //{
                //    msgBuilder.AppendLine(Message.Substring(index, 1));

                //    //if(this.Message.Substring(index, 1).Equals("\n") || index + 1 == (Message.Length))
                //    //{

                //        ev.Graphics.DrawString(msgBuilder.ToString(), 
                //                                       new Font("normal", this.MessageFontSize), Brushes.Blue,
                //                                        new Rectangle(this.RectLocationX + 13 * isNextLine,
                //                                                      this.RectLocationY,
                //                                                      400,
                //                                                      400),
                //                                        new StringFormat(MsgFormat));
                //        //X位置をインクリメント
                //        isNextLine--;
                //        msgBuilder.Remove(0, msgBuilder.Length);
                        
                    //}
                //}
                
                foreach(string line in MessageList)
                {
                    ev.Graphics.DrawString(line, 
                                                       new Font("normal", this.MessageFontSize), Brushes.Blue,
                                                        new Rectangle(this.CurrentLocationX + 13 * isNextLine,
                                                                      this.CurrentLocationY,
                                                                      400,
                                                                      400),
                                                        new StringFormat(MsgFormat));
                    isNextLine++;
                }
            }
            else
            {
                ev.Graphics.DrawString(this.Message, new Font("normal", this.MessageFontSize), Brushes.Blue,
                                                    new Rectangle(this.CurrentLocationX,
                                                                  this.CurrentLocationY,
                                                                  400,
                                                                  400),
                                                    new StringFormat(MsgFormat));
            }
        }

        private void drawDirectReSizeRect(PaintEventArgs ev)
        {
            Brush brush = null;
            Point[] points = null;
            if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_NO))
            {
                return;
            }
            else if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_V))
            {
                brush = Brushes.Blue;
                points = new Point[] {new Point(InnerRect.X + (InnerRect.Width / 2) - 5, InnerRect.Y + InnerRect.Height - 6), 
                                      new Point(InnerRect.X + (InnerRect.Width / 2) + 5, InnerRect.Y + InnerRect.Height - 6), 
                                      new Point(InnerRect.X + (InnerRect.Width / 2), InnerRect.Y + InnerRect.Height - 1) };
            }
            else if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_H))
            {
                brush = Brushes.Blue;
                points = new Point[] {new Point(InnerRect.X + InnerRect.Width - 6, InnerRect.Y + (InnerRect.Height / 2) - 5), 
                                      new Point(InnerRect.X + InnerRect.Width - 6, InnerRect.Y + (InnerRect.Height / 2) + 5), 
                                      new Point(InnerRect.X + InnerRect.Width - 1, InnerRect.Y + (InnerRect.Height / 2) - 1) };
            }
            else if (this.AllowReSizeStatus.Equals(PostCardLabelItemUnit.RESIZEABLE_BOTH))
            {
                brush = Brushes.LightGreen;
                points = new Point[] {new Point(InnerRect.X + InnerRect.Width - 12, InnerRect.Y + InnerRect.Height - 1), 
                                      new Point(InnerRect.X + InnerRect.Width - 1, InnerRect.Y + InnerRect.Height - 12), 
                                      new Point(InnerRect.X + InnerRect.Width - 1, InnerRect.Y + InnerRect.Height - 1) };
            }

            if (brush != null && points != null)
            {
                ev.Graphics.FillPolygon(brush, points);
            }

        }

        #endregion

    }

}
