using System;
using System.Collections.Generic;
using System.Linq;
using Katteker;
using KattekerCreator.Types;
using Semver;

namespace KattekerCreator
{
    public partial class SetupTemplate
    {
        public string AppName
        {
            get => _appName + ReleaseChannel;
            set => _appName = value;
        }

        public string Executable { get; set; }
        public string CompanyName { get; set; }
        public string Description { get; set; }
        public SemVersion Version { get; set; }
        public Version LegacyVersion => Version.ToSystemVersion();
        public string HelpUrl { get; set; }
        public long InstallSize { get; set; }
        public bool IsManaged { get; set; }
        public string IconPath { get; set; }
        public string SourcePath { get; set; } = string.Empty;
        public List<PhysicalFile> Files { get; set; } = new List<PhysicalFile>();
        private IEnumerable<string> Directories => DirectoriesToCreate();
        public string UserInterface { get; set; }
        public string OutFile { get; set; }
        public string UninstallIcon { get; set; }
        public string ReleaseChannel { get; set; }
        public string AssemblyName { get; set; }

        private readonly List<PathFragments> _directoriesList = new List<PathFragments>();
        private string _appName;

        private IEnumerable<string> DirectoriesToCreate()
        {
            foreach (var physicalFile in Files)
            {
                var dirSplit = new PathFragments(physicalFile.Target);
                if (dirSplit.FragmentLength > 1 && !_directoriesList.Contains(dirSplit))
                {
                    _directoriesList.Add(dirSplit);
                }
            }

            return _directoriesList.Select(x => x.ToString());
        }
    }
}