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

namespace PostCardHitsuji.DataClient.Model
{
    public class AddressContentModel : BaseContentModel, IDaoContentModel
    {
        private PropertyInfo propertyInfo_;
        
        public AddressContentModel()
        {
            this.Id = "";
            this.Enable = true.ToString().ToUpper();
            this.postalPre_ = "";
            this.postalPost_ = "";
            this.address1_ = "";
            this.address2_ = "";
        }

        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_ = ValueUtil.tryGetString(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 getAsStringIndexOf(object value, int index)
        {
            int target = index + 1;
            if (value == null || value == DBNull.Value) return "";
            return value.ToString().Length < target ? "" : value.ToString().Substring(target);
        }

        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, "");
            }
        }

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

        public override IContentModel DeepCopy()
        {
            AddressContentModel model = new AddressContentModel();
            model.Id = this.Id;
            model.Enable = this.Enable;
            model.postalPre = this.postalPre_;
            model.postalPost = this.postalPost_;
            model.address1_ = this.address1_;
            model.address2_ = this.address2_;

            return model;
        }

        public override List<AbsoluteDataResult> IsAbsoluteData()
        {
            var resultList = new List<AbsoluteDataResult>();
            AbsoluteDataResult result = null;
            if (postalPre_.Equals(""))
            {
                result = new AbsoluteDataResult();
                result.Result = ResultCodes.IsIrregular;
                result.name = "postalPre";
                result.org = postalPre_;
                result.Content = "〒番号（前）が空です。";
                resultList.Add(result);
            }
            if (postalPost_.Equals(""))
            {
                result = new AbsoluteDataResult();
                result.Result = ResultCodes.IsIrregular;
                result.name = "postalPost";
                result.org = postalPost_;
                result.Content = "〒番号（後）が空です。";
                resultList.Add(result);
            }
            if (address1_.Equals("") && address2_.Equals(""))
            {
                result = new AbsoluteDataResult();
                result.Result = ResultCodes.IsIrregular;
                result.name = "address1, address2";
                result.org = new List<string>() { address1_, address2_};
                result.Content = "住所１・住所２がともに空です。一つ以上の値を入力してください。";
                resultList.Add(result);
            }

            return resultList;
        }
    }
}
