/* Generated by MyraPad at 01/12/2020 11:46:54 */
using System.Linq;
using Myra.Graphics2D.TextureAtlases;
using Sledgemapper.Messages;
using Sledgemapper.Shared.Entities;
using TinyMessenger;

namespace Sledgemapper.UI
{
    public partial class NoteList
    {
        private readonly CommunicationManager CommunicationManager;
        private readonly TinyMessengerHub Messenger;

        public NoteList(CommunicationManager communicationManager, TinyMessengerHub messenger)
        {
            BuildUI();
            CommunicationManager = communicationManager;
            Messenger = messenger;
            for (var i = 0; i < CommunicationManager.SessionData.Notes.Values.Count; i++)
            {
                var note = CommunicationManager.SessionData.Notes.Values.ElementAt(i);
                var item = new NoteListItem();
                item.LblNoteText.Text = $"{note} - {note.Text}";
                item.BtnNoteCenter.Image = new TextureRegion(CachedContent.Instance.Location);
                item.BtnNoteView.Image = new TextureRegion(CachedContent.Instance.Eye);
                item.BtnNoteCenter.Click += (s, e) =>
                {
                    Messenger.Publish(new CenterOnTileMessage(this) { X = note.X, Y = note.Y });
                };

                item.BtnNoteView.Click += (s, e) => { EditNote(note); this.GetContainingWindow().Close(); };
                StackNotesList.AddChild(item);
            }
        }

        private void EditNote(Note note)
        {
            State.Instance.SelectedNote = new Note { X = note.X, Y = note.Y, Text = note.Text };
            new NoteWindow(CommunicationManager, note).ShowInModalWindow(Desktop, $" Note on {note.X}:{note.Y}");
        }
    }
}