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

namespace PostCardHitsuji.DataClient.Model
{
    public class PersonContentModel : BaseContentModel, IDaoContentModel
    {
        public enum PersonTypes
        {
            /// <summary>
            /// 宛先のモデルをあらわす
            /// （※発送者（Sender）のモデルを子として所有する）
            /// </summary>
            Reseiver,
            /// <summary>
            /// 発送者のモデルをあらわす
            /// </summary>
            Sender,
            /// <summary>
            /// パラメータとしてこのモデルを利用する場合など、宛先モデル・発送者モデルのどちらでもいい場合に指定
            /// （無くてもいいけど分かりやすくするため）
            /// </summary>
            Ignore
        }

        private PersonContentModel(){}
        public PersonContentModel(PersonTypes personType)
        {
            if (personType == PersonTypes.Reseiver)
                this.sender_ = new PersonContentModel(PersonTypes.Sender);

            Id = "";
            Enable = true.ToString().ToUpper();
            whoIs_ = "";
            name1_ = "";
            name2_ = "";
            name3_ = "";
            name4_ = "";
            namePost1_ = "";
            namePost2_ = "";
            namePost3_ = "";
            namePost4_ = "";
            address1_ = "";
            address2_ = "";
            tel_ = "";
            memo_ = "";
        }

        private string whoIs_;
        public string whoIs
        {
            get { return whoIs_; }
            set { whoIs_ = ValueUtil.tryGetString(value, ""); } }

        private string groupId_;
        public string groupId
        {
            get { return groupId_; }
            set { groupId_ = ValueUtil.tryGetString(value, ""); }
        }
        private string groupName_;
        public string groupName
        {
            get { return groupName_; }
            set { groupName_ = ValueUtil.tryGetString(value, ""); }
        }

        public string postalPre
        {
            get { return postalPre_; }
            set { postalPre_ = ValueUtil.tryGetString(value, ""); }
        }
        private string postalPre_;

        public string pre1 { get { return ValueUtil.getAsStringIndexOf(postalPre_, 0); } }
        public string pre2 { get { return ValueUtil.getAsStringIndexOf(postalPre_, 1); } }
        public string pre3 { get { return ValueUtil.getAsStringIndexOf(postalPre_, 2); } }

        public string postalPost
        {
            get { return postalPost_; }
            set { postalPost_ = value; }
        }
        private string postalPost_;

        public string post1 { get { return ValueUtil.getAsStringIndexOf(postalPost_, 0); } }
        public string post2 { get { return ValueUtil.getAsStringIndexOf(postalPost_, 1); } }
        public string post3 { get { return ValueUtil.getAsStringIndexOf(postalPost_, 2); } }
        public string post4 { get { return ValueUtil.getAsStringIndexOf(postalPost_, 3); } }

        private string name1_;
        public string name1
        {
            get { return name1_; }
            set { name1_ = ValueUtil.tryGetString(value, ""); }
        }
        private string name2_;
        public string name2
        {
            get { return name2_; }
            set { name2_ = ValueUtil.tryGetString(value, ""); }
        }
        private string name3_;
        public string name3
        {
            get { return name3_; }
            set { name3_ = ValueUtil.tryGetString(value, ""); }
        }
        private string name4_;
        public string name4
        {
            get { return name4_; }
            set { name4_ = ValueUtil.tryGetString(value, ""); }
        }

        private string namePost1_;
        public string namePost1
        {
            get { return namePost1_; }
            set { namePost1_ = ValueUtil.tryGetString(value, ""); }
        }
        private string namePost2_;
        public string namePost2
        {
            get { return namePost2_; }
            set { namePost2_ = ValueUtil.tryGetString(value, ""); }
        }
        private string namePost3_;
        public string namePost3
        {
            get { return namePost3_; }
            set { namePost3_ = ValueUtil.tryGetString(value, ""); }
        }
        private string namePost4_;
        public string namePost4
        {
            get { return namePost4_; }
            set { namePost4_ = ValueUtil.tryGetString(value, ""); }
        }

        private string address1_;
        public string address1
        {
            get { return address1_; }
            set { address1_ = ValueUtil.tryGetString(value, ""); }
        }
        private string address2_;
        public string address2
        {
            get { return address2_; }
            set { address2_ = ValueUtil.tryGetString(value, ""); }
        }
        private string address3_;
        public string address3
        {
            get { return address3_; }
            set { address3_ = ValueUtil.tryGetString(value, ""); }
        }

        private string tel_;
        public string tel
        {
            get { return tel_; }
            set { tel_ = ValueUtil.tryGetString(value, ""); }
        }

        private string memo_;
        public string memo
        {
            get { return memo_; }
            set { memo_ = ValueUtil.tryGetString(value, ""); }
        }

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

        private List<KeyValuePair<string, Image>> images_ = new List<KeyValuePair<string, Image>>();
        public List<KeyValuePair<string, Image>> images
        {
            get { return images_; }
            set { images_ = value; }
        }

        public PersonContentModel Sender
        {
            get { return sender_; }
            set { sender_ = value; }
        }
        private PersonContentModel sender_;

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

        public override IContentModel DeepCopy()
        {
            PersonContentModel model = new PersonContentModel();
            model.Id = this.Id;
            model.Enable = this.Enable;
            model.whoIs = this.whoIs_;
            model.groupId = this.groupId_;
            model.groupName = this.groupName_;
            model.postalPre = this.postalPre_;
            model.postalPost = this.postalPost_;
            model.address1 = this.address1_;
            model.address2 = this.address2_;
            model.address3 = this.address3_;
            model.name1_ = this.name1_;
            model.name2_ = this.name2_;
            model.name3_ = this.name3_;
            model.name4_ = this.name4_;
            model.namePost1 = this.namePost1_;
            model.namePost2 = this.namePost2_;
            model.namePost3 = this.namePost3_;
            model.namePost4 = this.namePost4_;
            model.tel = this.tel_;
            model.memo = this.memo_;
            model.Sender = this.sender_;
            return model;
        }

        public override List<AbsoluteDataResult> IsAbsoluteData()
        {
            var resultList = new List<AbsoluteDataResult>();
            AbsoluteDataResult result = null;
            if (groupId_.Equals("") || groupName_.Equals(""))
            {
                result = new AbsoluteDataResult();
                result.Result = ResultCodes.IsIrregular;
                result.name = "groupId_";
                result.org = new List<string> { groupId_ };
                result.Content = "グループを設定してください。";
                resultList.Add(result);
            }
            if (whoIs_.Equals(""))
            {
                result = new AbsoluteDataResult();
                result.Result = ResultCodes.IsIrregular;
                result.name = "whoIs_";
                result.org = new List<string> { whoIs_ };
                result.Content = "私/外の人 を設定してください。";
                resultList.Add(result);
            }
            if (name1_.Equals("") && name2_.Equals("") && name3_.Equals("") && name4_.Equals(""))
            {
                result = new AbsoluteDataResult();
                result.Result = ResultCodes.IsIrregular;
                result.name = "name1, name2, name3, name4";
                result.org = new List<string> { name1_, name2_, name3_, name4_ };
                result.Content = "名前を一つ以上入力してください。";
                resultList.Add(result);
            }

            return resultList;
        }

    }
}
