using System;
using System.Collections.Generic;
using Hitsuji.Framework.DataClient.Model;
using PostCardHitsuji.DataClient.DAO;
using System.Drawing;

namespace PostCardHitsuji.DataClient.Model
{
    public class MsgItemContentModel : BaseContentModel, IDaoContentModel
    {
        public const string ITEM_TYPE = "MESSAGE";

        public MsgItemContentModel()
        {
            Id = "";
            Enable = true.ToString().ToUpper();
            Name = "";
            message_ = "";
            fontName_ = Global.GlobalSettings.prepareFontNames()[0];
            size_ = "10";
            width_ = 15;
            height_ = 20;
            x_ = 0;
            y_ = 0;
            isMsgDirectVertical_ = false;
            font_ = new Font(fontName_, int.Parse(size_));
        }

        #region prim info

        public string Name
        {
            get;
            set;
        }

        #endregion //prim info


        #region msg info

        private string layoutId_;
        public string layoutId
        {
            get { return layoutId_; }
            set { layoutId_ = value; }
        }

        public int x
        {
            get { return x_; }
            set
            {
                x_ = value;
            }
        }
        private int x_;

        public int y
        {
            get { return y_; }
            set
            {
                y_ = value;
            }
        }
        private int y_;

        public int width
        {
            get { return width_; }
            set
            {
                width_ = value;
            }
        }
        private int width_;

        public int height
        {
            get { return height_; }
            set
            {
                height_ = value;
            }
        }
        private int height_;

        public string message
        {
            get { return message_; }
            set
            {
                message_ = value;
            }
        }
        private string message_;

        private string[] rtn_ = new string[2] {"\\r", "&return_;"};
        private string[] rtnn_ = new string[2] { "\\r\\n", "&returnn_;"};
        /// <summary>
        /// メッセージを取得する
        /// ※日本語縦表示は右から左に改行していくのでそれの対応
        /// </summary>
        public string messageMirror
        {
            get
            {
                if (message_.Equals("")) return "";

                var newMsg = message_.Replace(rtnn_[0], rtnn_[1]);
                newMsg = message_.Replace(rtn_[0], rtn_[1]);

                msgList.Clear();
                while (newMsg.IndexOf("\r\n") >= 0 || newMsg.IndexOf("\n") >= 0)
                {
                    //改行文字のインデックス、改行文字の文字数の変数
                    var findIndex = -1;
                    var targetLength = 0;
                    
                    //改行文字のインデックスと文字数を取得
                    var findIndex_rn = newMsg.IndexOf("\r\n");
                    var findIndex_r = newMsg.IndexOf("\n");
                    findIndex = findIndex_rn == -1 ? findIndex_r :
                                findIndex_r == -1 ? findIndex_rn :
                                Math.Min(findIndex_rn, findIndex_r);
                    targetLength = findIndex == findIndex_rn ? 4 : 2;

                    //1行分の文字列を抽出する（= 改行文字までの文字列 ＋ 改行文字列）
                    var lineText = newMsg.Substring(0, (findIndex - 1) + targetLength);
                    //1行分の文字列から改行文字列を削除
                    lineText = lineText.Replace("\r\n", "").Replace("\n", "");
                    //結果リストに追加
                    msgList.Add(lineText);
                    //1行分の文字列を全体の文字列から削除
                    newMsg = newMsg.Substring((findIndex - 1) + targetLength, newMsg.Length - (findIndex + targetLength) + 1);
                }
                msgList.Add(newMsg);

                newMsg = "";
                //結果リストの行を逆向きに並び替える
                for (int ind = msgList.Count - 1; ind >= 0; ind--)
                {
                    newMsg += msgList[ind];
                    if (ind != 0) newMsg += "\r\n";
                }
                return newMsg;
            }
        }
        List<string> msgList = new List<string>();

        /// <summary>
        /// 文字のサイズ ×　行数
        /// ミラーメッセージ取得時に、文字列全体をずらすための値
        /// </summary>
        public int getMirroringWidth
        {
            get 
            {
                if (msgList == null || msgList.Count == 0) return 0;
                return 0;
            }
        }

        public string fontName
        {
            get { return fontName_; }
            set
            {
                fontName_ = string.IsNullOrEmpty(value) ? Global.GlobalSettings.prepareFontNames()[0] : value;
                onFontPropertyChanged();
            }
        }
        private string fontName_ = "NORMAL";

        public string size
        {
            get { return size_; }
            set
            {
                size_ = value;
                onFontPropertyChanged();
            }
        }
        private string size_ = "10";

        public bool isMsgDirectVertical
        {
            get { return isMsgDirectVertical_; }
            set
            {
                isMsgDirectVertical_ = value;
                onStringFormatPropertyChanged();
            }
        }
        private bool isMsgDirectVertical_;

        public StringFormat msgFormat { get { return msgVertical_; } }
        private StringFormat msgVertical_ = new StringFormat();


        public Color FontColor
        {
            get { return fontColor_; }
            set { fontColor_ = value; }
        }
        private Color fontColor_ = Color.Black;

        private Font font_;
        public Font font
        {
            get 
            { 
                return font_; 
            }
        }

        #endregion //msg info

        private void onFontPropertyChanged()
        {
            font_ = new Font(fontName_, int.Parse(size_));
        }
        private void onStringFormatPropertyChanged()
        {
            msgFormat.FormatFlags = isMsgDirectVertical_ == true 
                                        ? StringFormatFlags.DirectionVertical 
                                        : StringFormatFlags.NoWrap;
        }

        protected override void onBasePropertyChanged(string propertyName, object oldValue, object newValue)
        {
            //throw new NotImplementedException();
        }

        public override List<AbsoluteDataResult> IsAbsoluteData()
        {
            throw new NotImplementedException();
        }

        public override IContentModel DeepCopy()
        {
            MsgItemContentModel clone = new MsgItemContentModel();
            clone.Id = this.Id;
            clone.Enable = this.Enable;
            clone.message = this.message_;
            clone.FontColor = this.fontColor_;
            clone.size = this.size_;
            return clone;
        }
    }
}
