﻿// ###################### | #############################################################
// CLASS ................ | SOFTWARE DEVELOPMENT CAPSTONE (C868) ........................
// ASSIGNMENT ........... | C# PERFORMANCE ASSESSMENT ...................................
// TASK ................. | EZP1 TASK 2: SOFTWARE APPLICATION AND DOCUMENTATION .........
// ###################### | #############################################################
// WGU STUDENT .......... | Mark K Ewer .................................................
// STUDENT ID ........... | 001094518 ...................................................
// DEGREE PROGRAM ....... | B.S. in Software Development ................................
// ENROLLED ON .......... | Feb 1, 2019 .................................................
// MENTOR ............... | Becky Stovall ...............................................
// PHONE ................ | (904) 238-7347 ..............................................
// TIME ZONE ............ | Eastern Time ................................................
// EMAIL ................ | MEwer@wgu.edu or Mark@3w3r.com ..............................
// WEB SITE ............. | http://www.markewer.com .....................................
// ###################### | #############################################################
namespace EWER_C868.Core.Entities
{
    using System;
    using System.Collections.Generic;
    using Ewer.Cqrs.Aggregates;
    using Ewer.Cqrs.Events;
    using EWER_C868.Core.Excptions;
    using EWER_C868.Core.ProductManager.Events;

    public class Product : AggregateRoot
        //PRODUCT MANAGER
        , IEmit<NewProductCreated>
        , IEmit<ProductDeleted>
        , IEmit<ProductRenamed>
    {
        protected bool IsDeleted { get; set; } = false;
        protected Models.ProductDetails Model { get; set; }

        public void Apply(NewProductCreated e)
        {
            this.Model = new Models.ProductDetails();
            this.Model.Id = e.AggregateRootId;
            this.Model.Name = e.Name;
        }

        public void Apply(ProductDeleted e)
        {
            this.IsDeleted = true;
        }

        public void Apply(ProductRenamed e)
        {
            this.Model.Name = e.Name;
            this.Model.Description = e.Description;
        }

        public void Initialize(string name)
        {
            Emit(new NewProductCreated(this.Id, name));
        }

        internal void Delete()
        {
            Emit(new ProductDeleted(this.Id));
        }

        internal void Rename(string name, string description)
        {
            if (string.IsNullOrEmpty(name)) throw new InvalidNameException("This product name is not valid.");
            this.Emit(new ProductRenamed(this.Id, name, description));
        }
    }
}
