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

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

        public IllustItemContentModel()
        {
            Id = "";
            Enable = false.ToString().ToUpper();
            x_ = 1;
            y_ = 1;
            width_ = 200;
            height_ = 200;
            filepath_ = "";
            illust_ = null;
            multiple_ = "1";
        }

        #region prim info

        public string Name
        {
            get;
            set;
        }

        #endregion //prim info

        #region illust 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 filepath
        {
            get { return filepath_; }
            set
            {
                filepath_ = value;
                onIllustPropertyChanged();
            }
        }
        private string filepath_;

        public Image illust
        {
            get { return illust_; }
            set
            {
                illust_ = value;
            }
        }
        private Image illust_;
        
        public string multiple
        {
            get { return multiple_; }
            set
            {
                multiple_ = value;
                onIllustPropertyChanged();
            }
        }
        private string multiple_;

        #endregion //illust info

        public void onIllustPropertyChanged()
        {
            double tempMultiple = 1;
            if (!double.TryParse(multiple_, out tempMultiple)) multiple_ = "1";
            else multiple_ = multiple_.Equals("0") ? "1" : multiple_;

            if (filepath_ == null || filepath_.Equals("") || !System.IO.File.Exists(filepath_))
            {
                illust = null;
                width_ = 200;
                height_ = 200;
                return;
            }

            illust_ = ReSizeImageUtil.reSizeImage(System.Drawing.Image.FromFile(filepath_), double.Parse(multiple));
            width_ = illust_.Width;
            height_ = illust_.Height;
        }

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

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

        public override IContentModel DeepCopy()
        {
            var clone = new IllustItemContentModel();
            clone.Id = this.Id;
            clone.Enable = this.Enable;
            clone.filepath_ = this.filepath_;
            clone.illust_ = this.illust;
            clone.x_ = this.x;
            clone.y_ = this.y;
            clone.width_ = this.width;
            clone.height_ = this.height;
            return clone;
        }
    }
}
