﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.Audio;
using Windows.Media.Render;
using Windows.Storage;
using Windows.ApplicationModel;

namespace RaceGame.Systems
{
    sealed class AudioPlayer : GameSystem
    {
        private AudioGraph m_graph;
        private AudioDeviceOutputNode m_outputDevice;

        private Dictionary<string, AudioFileInputNode> m_fileInputs =
            new Dictionary<string, AudioFileInputNode>();


        public async Task InitSystem()
        {
            var settings = new AudioGraphSettings(AudioRenderCategory.Media);
            var graphResult = await AudioGraph.CreateAsync(settings);

            if (graphResult.Status == AudioGraphCreationStatus.Success)
            {
                m_graph = graphResult.Graph;

                var outputDeviceResult = await m_graph.CreateDeviceOutputNodeAsync();

                if (outputDeviceResult.Status == AudioDeviceNodeCreationStatus.Success)
                {
                    m_outputDevice = outputDeviceResult.DeviceOutputNode;
                }
            }
        }

        public async Task AddSoundFile(string uri)
        {
            var soundFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));
            var inputNodeResult = await m_graph.CreateFileInputNodeAsync(soundFile);

            if (inputNodeResult.Status == AudioFileNodeCreationStatus.Success)
            {
                var inputNode = inputNodeResult.FileInputNode;

                m_fileInputs.Add(soundFile.Name, inputNode);
                inputNode.Stop();
                inputNode.AddOutgoingConnection(m_outputDevice);
            }
        }

        public void Play(string audioName)
        {
            var audio = m_fileInputs[audioName];
            audio.Reset();
            audio.Start();
        }
    }







    //sealed class AudioPlayer
    //{
    //    private Dictionary<string, AudioFileInputNode> m_audioNodes =
    //        new Dictionary<string, AudioFileInputNode>();








    //    private AudioGraph m_audioGraph;
    //    private AudioSubmixNode m_submixNode;

    //    private AudioFileInputNode m_inputNode;
    //    private AudioDeviceOutputNode m_outputNode;


    //    private Dictionary<string, string> m_audioSources =
    //        new Dictionary<string, string>()
    //        {
    //            { "Rocket", "Rocket.wav" },
    //            { "Explosion", "Explosion.wav" },
    //            //{ "GameMusic", "GameMusic.wav" }
    //        };


    //    public async Task Load()
    //    {
    //        var settings = new AudioGraphSettings(AudioRenderCategory.GameMedia);

    //        var graphResult = await AudioGraph.CreateAsync(settings);
    //        if (graphResult.Status != AudioGraphCreationStatus.Success)
    //        {
    //            throw new Exception("Tjsa...");
    //            // TODO: Do Something different here.
    //            // graphResult.Status.ToString())
    //        }
    //        m_audioGraph = graphResult.Graph;

    //        var outputResult = await m_audioGraph.CreateDeviceOutputNodeAsync();
    //        if (outputResult.Status != AudioDeviceNodeCreationStatus.Success)
    //        {
    //            throw new Exception("Jammer...");
    //            // TODO: Do something different here.
    //        }
    //        m_outputNode = outputResult.DeviceOutputNode;

    //        //m_submixNode = m_audioGraph.CreateSubmixNode();
    //        m_submixNode.AddOutgoingConnection(m_outputNode);

    //        var folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets\\Audio");
    //        foreach (var source in m_audioSources)
    //        {
    //            var file = await folder.GetFileAsync(source.Value);

    //            var inputResult = await m_audioGraph.CreateFileInputNodeAsync(file);
    //            if (inputResult.Status != AudioFileNodeCreationStatus.Success)
    //            {
    //                throw new Exception(":(");
    //                // TODO: Do something different here.
    //            }

    //            m_inputNode = inputResult.FileInputNode;
    //            m_inputNode.AddOutgoingConnection(m_submixNode);
    //            m_inputNode.AddOutgoingConnection(m_outputNode);
    //            m_audioNodes.Add(source.Key, m_inputNode);
    //        }

    //    }

    //    public void Play(string name)
    //    {
    //        m_inputNode.Start();
    //    }
}

