using Sentry;
using System;
using TinyMessenger;

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

        public CampaignWindow(CommunicationManager communicationManager, TinyMessengerHub messenger)
		{
			BuildUI();
            CommunicationManager = communicationManager;
            Messenger = messenger;
            BtnNewCampaign.Click += OnButtonNewCampaignClicked;
        }

        private async void OnButtonNewCampaignClicked(object sender, EventArgs e)
        {
            //var localContent = ((TextButton)sender).GetParentContentInWindow<CampaignWindow>();// localWindow.Content as PlayerWindow;

            var isValid = TxtCampaign.ValidateTextbox();
            if (!isValid)
            {
                return;
            }

            //if (CommunicationManager.Connection.State != HubConnectionState.Connected)
            //{
            //    lblConnectionStatus.Text = "Connecting";
            //    await CommunicationManager.Connection.StartAsync();
            //    UpdateConnectionState(CommunicationManager.Connection);
            //}

            var successful = false;
            try
            {
                await CommunicationManager.Api.NewCampaign(TxtCampaign.Text);

                //if (result)
                //{
                //    CommunicationManager.SessionData.SessionName = localContent.TxtSession.Text;
                //    CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded;
                //    CommunicationManager.SessionData.MapEntityDeleted -= OnMapEntityDeleted;
                //    CommunicationManager.SessionData.MapEntityAdded += OnMapEntityAdded;
                //    CommunicationManager.SessionData.MapEntityDeleted += OnMapEntityDeleted;
                //}
                //successful = result;
                //var result2 = await CommunicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.TxtSession.Text);
                //CommunicationManager.SessionData.SessionId = result2.SessionId;
                //CommunicationManager.SessionData.SessionName = localContent.TxtSession.Text;
            }
            catch (Exception ex)
            {
                SentrySdk.CaptureException(ex);
                //ExceptionlessClient.Default.SubmitException(ex);
            }

            //if (successful)
            //{
            //    //CommunicationManager.SessionData.SessionName = localContent.TxtSession.Text;
            //    //CommunicationManager.SessionData.Map = CommunicationManager.SessionData.Map;
            //    //CommunicationManager.SessionData.Overlays = CommunicationManager.SessionData.Overlays;
            //    //CommunicationManager.SessionData.Walls = CommunicationManager.SessionData.Walls;
            //    //lblSessionName.Text = CommunicationManager.SessionData.SessionName;
            //    //MenuConnectSync.Enabled = true;
            //    //MenuConnectUpload.Enabled = true;
            //    localWindow.Close();
            //}

            this.GetContainingWindow().Close();

        }
    }
}