// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE

using System;
using System.Linq;
using System.Threading;
using OpenTK;
using OpenTK.Input;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Transforms;
using osu.Framework.Input;
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Modes;
using osu.Game.Overlays;
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Select.Options;

namespace osu.Game.Screens.Select
{
    public abstract class SongSelect : OsuScreen
    {
        private Bindable<PlayMode> playMode = new Bindable<PlayMode>();
        private BeatmapDatabase database;
        protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap(Beatmap);

        private BeatmapCarousel carousel;
        private TrackManager trackManager;
        private DialogOverlay dialogOverlay;

        private static readonly Vector2 wedged_container_size = new Vector2(0.5f, 225);
        private BeatmapInfoWedge beatmapInfoWedge;

        private static readonly Vector2 background_blur = new Vector2(20);
        private CancellationTokenSource initialAddSetsTask;

        private SampleChannel sampleChangeDifficulty;
        private SampleChannel sampleChangeBeatmap;

        protected virtual bool ShowFooter => true;

        /// <summary>
        /// Can be null if <see cref="ShowFooter"/> is false.
        /// </summary>
        protected readonly BeatmapOptionsOverlay BeatmapOptions;

        /// <summary>
        /// Can be null if <see cref="ShowFooter"/> is false.
        /// </summary>
        protected readonly Footer Footer;

        public readonly FilterControl FilterControl;

        protected SongSelect()
        {
            const float carousel_width = 640;
            const float filter_height = 100;

            Add(new ParallaxContainer
            {
                Padding = new MarginPadding { Top = filter_height },
                ParallaxAmount = 0.005f,
                RelativeSizeAxes = Axes.Both,
                Children = new[]
                {
                    new WedgeBackground
                    {
                        RelativeSizeAxes = Axes.Both,
                        Padding = new MarginPadding { Right = carousel_width * 0.76f },
                    }
                }
            });
            Add(carousel = new BeatmapCarousel
            {
                RelativeSizeAxes = Axes.Y,
                Size = new Vector2(carousel_width, 1),
                Anchor = Anchor.CentreRight,
                Origin = Anchor.CentreRight,
                SelectionChanged = selectionChanged,
                StartRequested = raiseSelect
            });
            Add(FilterControl = new FilterControl
            {
                RelativeSizeAxes = Axes.X,
                Height = filter_height,
                FilterChanged = criteria => filterChanged(criteria),
                Exit = Exit,
            });
            Add(beatmapInfoWedge = new BeatmapInfoWedge
            {
                Alpha = 0,
                Size = wedged_container_size,
                RelativeSizeAxes = Axes.X,
                Margin = new MarginPadding
                {
                    Top = 20,
                    Right = 20,
                },
                X = -50,
            });

            if (ShowFooter)
            {
                Add(BeatmapOptions = new BeatmapOptionsOverlay
                {
                    Margin = new MarginPadding
                    {
                        Bottom = 50,
                    },
                });
                Add(Footer = new Footer
                {
                    OnBack = Exit,
                    OnStart = raiseSelect,
                });
            }
        }

        [BackgroundDependencyLoader(permitNulls: true)]
        private void load(BeatmapDatabase beatmaps, AudioManager audio, DialogOverlay dialog, OsuGame osu, OsuColour colours)
        {
            if (Footer != null)
            {
                Footer.AddButton(@"random", colours.Green, SelectRandom, Key.F2);
                Footer.AddButton(@"options", colours.Blue, BeatmapOptions.ToggleVisibility, Key.F3);

                BeatmapOptions.AddButton(@"Delete", @"Beatmap", FontAwesome.fa_trash, colours.Pink, promptDelete, Key.Number4, float.MaxValue);
            }

            if (osu != null)
                playMode.BindTo(osu.PlayMode);
            playMode.ValueChanged += playMode_ValueChanged;

            if (database == null)
                database = beatmaps;

            database.BeatmapSetAdded += onBeatmapSetAdded;
            database.BeatmapSetRemoved += onBeatmapSetRemoved;

            trackManager = audio.Track;
            dialogOverlay = dialog;

            sampleChangeDifficulty = audio.Sample.Get(@"SongSelect/select-difficulty");
            sampleChangeBeatmap = audio.Sample.Get(@"SongSelect/select-expand");

            initialAddSetsTask = new CancellationTokenSource();

            carousel.BeatmapsChanged = beatmapsLoaded;
            carousel.Beatmaps = database.Query<BeatmapSetInfo>().Where(b => !b.DeletePending);
        }

        private void beatmapsLoaded()
        {
            if (Beatmap != null)
                carousel.SelectBeatmap(Beatmap.BeatmapInfo, false);
            else
                carousel.SelectNext();
        }

        private void raiseSelect()
        {
            if (Beatmap == null) return;

            Beatmap.PreferredPlayMode = playMode.Value;
            OnSelected();
        }

        public void SelectRandom() => carousel.SelectRandom();

        protected abstract void OnSelected();

        private void filterChanged(FilterCriteria criteria, bool debounce = true)
        {
            carousel.Filter(criteria, debounce);
        }

        private void onBeatmapSetAdded(BeatmapSetInfo s) => carousel.AddBeatmap(s);

        private void onBeatmapSetRemoved(BeatmapSetInfo s) => Schedule(() => removeBeatmapSet(s));

        protected override void OnEntering(Screen last)
        {
            base.OnEntering(last);
            ensurePlayingSelected();

            changeBackground(Beatmap);

            Content.FadeInFromZero(250);

            beatmapInfoWedge.State = Visibility.Visible;

            FilterControl.Activate();
        }

        protected override void OnResuming(Screen last)
        {
            changeBackground(Beatmap);
            ensurePlayingSelected();
            base.OnResuming(last);

            Content.FadeIn(250);

            Content.ScaleTo(1, 250, EasingTypes.OutSine);

            FilterControl.Activate();
        }

        protected override void OnSuspending(Screen next)
        {
            Content.ScaleTo(1.1f, 250, EasingTypes.InSine);

            Content.FadeOut(250);

            FilterControl.Deactivate();
            base.OnSuspending(next);
        }

        protected override bool OnExiting(Screen next)
        {
            beatmapInfoWedge.State = Visibility.Hidden;

            Content.FadeOut(100);

            FilterControl.Deactivate();
            return base.OnExiting(next);
        }

        protected override void Dispose(bool isDisposing)
        {
            base.Dispose(isDisposing);

            database.BeatmapSetAdded -= onBeatmapSetAdded;
            database.BeatmapSetRemoved -= onBeatmapSetRemoved;

            initialAddSetsTask.Cancel();
        }

        private void playMode_ValueChanged(object sender, EventArgs e) => carousel.Filter();

        private void changeBackground(WorkingBeatmap beatmap)
        {
            var backgroundModeBeatmap = Background as BackgroundScreenBeatmap;
            if (backgroundModeBeatmap != null)
            {
                backgroundModeBeatmap.Beatmap = beatmap;
                backgroundModeBeatmap.BlurTo(background_blur, 1000);
                backgroundModeBeatmap.FadeTo(1, 250);
            }

            beatmapInfoWedge.UpdateBeatmap(beatmap);
        }

        /// <summary>
        /// The global Beatmap was changed.
        /// </summary>
        protected override void OnBeatmapChanged(WorkingBeatmap beatmap)
        {
            base.OnBeatmapChanged(beatmap);

            //todo: change background in selectionChanged instead; support per-difficulty backgrounds.
            changeBackground(beatmap);
            carousel.SelectBeatmap(beatmap?.BeatmapInfo);
        }

        /// <summary>
        /// selection has been changed as the result of interaction with the carousel.
        /// </summary>
        private void selectionChanged(BeatmapGroup group, BeatmapInfo beatmap)
        {
            bool beatmapSetChange = false;

            if (!beatmap.Equals(Beatmap?.BeatmapInfo))
            {
                if (beatmap.BeatmapSetInfoID == Beatmap?.BeatmapInfo.BeatmapSetInfoID)
                    sampleChangeDifficulty.Play();
                else
                {
                    sampleChangeBeatmap.Play();
                    beatmapSetChange = true;
                }
                Beatmap = database.GetWorkingBeatmap(beatmap, Beatmap);
            }
            ensurePlayingSelected(beatmapSetChange);
        }

        private void ensurePlayingSelected(bool preview = false)
        {
            Track track = Beatmap?.Track;

            if (track != null)
            {
                trackManager.SetExclusive(track);
                if (preview)
                    track.Seek(Beatmap.Beatmap.Metadata.PreviewTime);
                track.Start();
            }
        }

        private void selectBeatmap(BeatmapSetInfo beatmapSet = null)
        {
            carousel.SelectBeatmap(beatmapSet != null ? beatmapSet.Beatmaps.First() : Beatmap?.BeatmapInfo);
        }

        private void removeBeatmapSet(BeatmapSetInfo beatmapSet)
        {
            carousel.RemoveBeatmap(beatmapSet);
            if (carousel.SelectedBeatmap == null)
                Beatmap = null;
        }

        private void promptDelete()
        {
            if (Beatmap != null)
                dialogOverlay?.Push(new BeatmapDeleteDialog(Beatmap));
        }

        protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
        {
            if (args.Repeat) return false;

            switch (args.Key)
            {
                case Key.Enter:
                    raiseSelect();
                    return true;
                case Key.Delete:
                    if (state.Keyboard.ShiftPressed)
                    {
                        promptDelete();
                        return true;
                    }
                    break;
            }

            return base.OnKeyDown(state, args);
        }
    }
}