﻿using System.Collections.Generic;
using System.Collections.Immutable;

namespace VioletGrass.ProjectModel
{
    /// <summary>
    /// The owner of a project, either a Person or a Organization.
    /// </summary>
    public abstract class Owner
    {
        protected Owner(string name, string displayName, string description, string webPageUrl, string email, string imageUrl, bool isPublic, IEnumerable<Project> projects)
        {
            Name = name;
            DisplayName = displayName;
            Description = description;
            WebPageUrl = webPageUrl;
            Email = email;
            ImageUrl = imageUrl;
            IsPublic = isPublic;

            Projects = projects.ToImmutableArray();
        }

        protected abstract Owner Clone(string name, string displayName, string description, string webPageUrl, string email, string imageUrl, bool isPublic, IEnumerable<Project> projects);

        /// <summary>
        /// The name of the owner. Is the technical primary identifier
        /// </summary>
        public string Name { get; }
        /// <summary>
        /// The display name of the owner. This name is used for displaying purposes.
        /// </summary>
        public string DisplayName { get; }
        /// <summary>
        /// Description of the owner, e.g. the organization's vision.
        /// </summary>
        public string Description { get; }
        /// <summary>
        /// The url of the web page of the owner.
        /// </summary>
        public string WebPageUrl { get; }
        /// <summary>
        /// The email of the owner.
        /// </summary>
        public string Email { get; }
        /// <summary>
        /// The image url of the web page of the owner.
        /// </summary>
        public string ImageUrl { get; }
        /// <summary>
        /// Information whether the owner is public visible.
        /// </summary>
        public bool IsPublic { get; }

        /// <summary>
        /// The list of all projects of the owner.
        /// </summary>
        public ImmutableArray<Project> Projects { get; }

        /// <summary>
        /// Creates a copy of the owner with a new project added.
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public Owner WithProject(Project project)
        {
            var newProjects = Projects.Add(project);

            return WithProjects(newProjects);
        }

        /// <summary>
        /// Creates a copy of the owner, replacing its projects.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public Owner WithProjects(IEnumerable<Project> projects)
        {
            var newOwner = Clone(Name, DisplayName, Description, WebPageUrl, Email, ImageUrl, IsPublic, projects);

            return newOwner;
        }
    }
}