﻿// This file is part of AbstractCode.
// 
// AbstractCode is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// AbstractCode is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with AbstractCode.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AbstractCode.Collections.Generic;
using AbstractCode.Symbols;

namespace AbstractCode.Ast
{
    public class SourceAssembly : AssemblyDefinition
    {
        public SourceAssembly(string name)
        {
            Version = new Version(1, 0, 0, 0);
            Name = name;
            SourceFiles = new EventBasedCollection<SourceFile>();

            SourceFiles.InsertingItem += SourceFilesOnInsertingItem;
            SourceFiles.InsertedItem += SourceFilesOnInsertedItem;
            SourceFiles.RemovedItem += SourceFilesOnRemovedItem;
        }

        public override string Name
        {
            get;
        }

        public override Version Version
        {
            get;
        }

        public override string FilePath
        {
            get { return string.Empty; }
        }

        public EventBasedCollection<SourceFile> SourceFiles
        {
            get;
        }

        private static void SourceFilesOnInsertingItem(object sender, CollectionChangingEventArgs<SourceFile> e)
        {
            if (e.TargetObject.Assembly != null)
            {
                e.Cancel = true;
                throw new InvalidOperationException("Source file is already added to another assembly.");
            }
        }

        private void SourceFilesOnInsertedItem(object sender, CollectionChangedEventArgs<SourceFile> e)
        {
            e.TargetObject.Assembly = this;
        }

        private static void SourceFilesOnRemovedItem(object sender, CollectionChangedEventArgs<SourceFile> e)
        {
            e.TargetObject.Assembly = null;
        }


        public override IEnumerable<NamespaceDefinition> GetNamespaceDefinitions()
        {
            var merged = new Dictionary<string, IList<NamespaceDefinition>>();

            foreach (var file in SourceFiles)
            {
                foreach (var toplevelNamespace in file.CompilationUnit.GetNamespaceDefinitions())
                {
                    IList<NamespaceDefinition> list;
                    if (!merged.TryGetValue(toplevelNamespace.Name, out list))
                        merged.Add(toplevelNamespace.Name, list = new List<NamespaceDefinition>());

                    list.Add(toplevelNamespace);
                }
            }

            return merged.Values.Select(list => new MergedNamespaceDefinition(list));
        }

        public override IEnumerable<TypeDefinition> GetTypeDefinitions()
        {
            return SourceFiles.SelectMany(file => file.CompilationUnit.GetTypeDefinitions());
        }
    }
}
