using System.ComponentModel;

using BS.Shared;
using BS.Shared.DataContracts;
using System.Collections.Generic;

namespace BeWo.ViewModel
{
    public class BeWoFolderVM : AbstractDCMapperVM<BeWoFolderDC>
    {
        public static string PropertyName_Files = "Files";

        public static string PropertyName_Name = "Name";

        public static string PropertyName_ObjectOid = "ObjectOid";

        public static string PropertyName_ObjectTid = "ObjectTid";

        public static string PropertyName_ParentFolder = "ParentFolder";

        public static string PropertyName_SubFolders = "SubFolders";

        private BindingList<FileAttachmentVM> _Files;

        private string _Name;

        private long? _ObjectOid;

        private TableID _ObjectTid;

        private BeWoFolderVM _ParentFolder;

        private BindingList<BeWoFolderVM> _SubFolders;

        public BeWoFolderVM(BeWoFolderDC pDC) : base(pDC, true)
        {
        }

        public BindingList<FileAttachmentVM> Files
        {
            get { return this._Files ?? (this._Files = new BindingList<FileAttachmentVM>()); }

            set
            {
                if (this.AreDifferent(this._Files, value))
                {
                    this._Files = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Files, value), PropertyName_Files);
                    this.FirePropertyChanged(PropertyName_Files);
                }
            }
        }

        public string Name
        {
            get { return this._Name; }

            set
            {
                if (this.AreDifferent(this._Name, value))
                {
                    this._Name = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Name, value), PropertyName_Name);
                    this.FirePropertyChanged(PropertyName_Name);
                }
            }
        }

        public long? ObjectOid
        {
            get { return this._ObjectOid; }

            set
            {
                if (this.AreDifferent(this._ObjectOid, value))
                {
                    this._ObjectOid = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ObjectOid, value), PropertyName_ObjectOid);
                    this.FirePropertyChanged(PropertyName_ObjectOid);
                }
            }
        }

        public TableID ObjectTid
        {
            get { return this._ObjectTid; }

            set
            {
                if (this.AreDifferent(this._ObjectTid, value))
                {
                    this._ObjectTid = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ObjectTid, value), PropertyName_ObjectTid);
                    this.FirePropertyChanged(PropertyName_ObjectTid);
                }
            }
        }

        public BeWoFolderVM ParentFolder
        {
            get { return this._ParentFolder; }

            set
            {
                if (this.AreDifferent(this._ParentFolder, value))
                {
                    this._ParentFolder = value;

                    // StoreDirtyInformation(AreDifferent(DataContract.ParentFolder, value), PropertyName_ParentFolder);
                    this.FirePropertyChanged(PropertyName_ParentFolder);
                }
            }
        }

        public BindingList<BeWoFolderVM> SubFolders
        {
            get { return this._SubFolders ?? (this._SubFolders = new BindingList<BeWoFolderVM>()); }

            set
            {
                if (this.AreDifferent(this._SubFolders, value))
                {
                    this._SubFolders = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.SubFolders, value), PropertyName_SubFolders);
                    this.FirePropertyChanged(PropertyName_SubFolders);
                }
            }
        }

        protected override void InitByDataContract(BeWoFolderDC pDataContract)
        {
            _Files = new BindingList<FileAttachmentVM>();
            if (DataContract.Files != null)
            {
                foreach (var item in DataContract.Files)
                {
                    FileAttachmentVM fileVM = new FileAttachmentVM(item);
                    fileVM.Folder = this;
                    _Files.Add(fileVM);

                }
            }
            _SubFolders = new BindingList<BeWoFolderVM>();
            if (DataContract.SubFolders != null)
            {
                foreach (var item in DataContract.SubFolders)
                {
                    BeWoFolderVM subfolderVM = new BeWoFolderVM(item);
                    subfolderVM.ParentFolder = this;
                    _SubFolders.Add(subfolderVM);
                }
            }
            this._ObjectOid = pDataContract.ObjectOid;
            this._Name = pDataContract.Name;
            this._ObjectTid = pDataContract.ObjectTid;
        }

        protected override BeWoFolderDC MapToDataContract(BeWoFolderDC pDataContract, bool doCommit)
        {

            DataContract.Files = new List<FileAttachmentDC>();
            foreach (var item in _Files)
            {
                DataContract.Files.Add(item.CommitToDataContract());
            }
            DataContract.SubFolders = new List<BeWoFolderDC>();
            foreach (var item in _SubFolders)
            {
                DataContract.SubFolders.Add(item.CommitToDataContract());
            }
            if (_ParentFolder != null)
            {
                pDataContract.ParentBeWoFolderOid = _ParentFolder.DataContract.BeWoFolderOid;
                pDataContract.ObjectOid = _ParentFolder.DataContract.ObjectOid;
                pDataContract.ObjectTid = _ParentFolder.DataContract.ObjectTid;
            }
            
            pDataContract.Name = this._Name;
            
            return pDataContract;
        }
    }
}