using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace Aurora.Design.Components.HostSelector
{
    public enum ConnectionType
    {
        Host,
        Client,
    }

    public enum SelectorState
    {
        SelectingHost,
        EnteringCredentials,
    }

    public partial class HostSelector : ContentView
    {

        public HostSelector()
        {
            InitializeComponent();
            //Set initial conditions
            CredentialEditorLayout.IsVisible = true;

            buttonHost.Clicked += OnButtonHostClicked;
            buttonClient.Clicked += OnButtonClientClicked;
            HostnameEntry.TextChanged += (sender, e) =>
            {
                Hostname = e.NewTextValue;
            };
        }


        /// <summary>
        /// On the host button clicked.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        void OnButtonHostClicked(object sender, EventArgs e)
        {
            if (HostCommand.CanExecute(null))
            {
                HostCommand.Execute(null);
            }
        }

        /// <summary>
        /// On the client button clicked.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        void OnButtonClientClicked(object sender, EventArgs e)
        {
            if (JoinCommand.CanExecute(null))
            {
                JoinCommand.Execute(null);
            }
        }

        #region Host Selected Command
        public static readonly BindableProperty HostCommandProperty =
            BindableProperty.Create(propertyName: "HostSelectedCommand",
                returnType: typeof(Command),
                declaringType: typeof(HostSelector));

        public Command HostCommand
        {
            get { return (Command)GetValue(HostCommandProperty); }
            set { SetValue(HostCommandProperty, value); }
        }

        #endregion Host Selected Command

        #region Client Selected Command
        public static readonly BindableProperty JoinCommandProperty =
            BindableProperty.Create(propertyName: "JoinSelectedCommand",
            returnType: typeof(Command),
            declaringType: typeof(HostSelector));

        public Command JoinCommand
        {
            get { return (Command)GetValue(JoinCommandProperty); }
            set { SetValue(JoinCommandProperty, value); }
        }

        #endregion Client Selected Command

        #region Hostname property
        public static readonly BindableProperty HostnameProperty =
            BindableProperty.Create(propertyName: "Hostname",
            returnType: typeof(string),
            declaringType: typeof(HostSelector),
            defaultBindingMode: BindingMode.TwoWay,
            propertyChanged: OnHostNameChanged);

        public string Hostname
        {
            get { return (string)GetValue(HostnameProperty); }
            set { SetValue(HostnameProperty, value); }
        }

        private static void OnHostNameChanged(BindableObject bindable, object oldValue, object newValue)
        {
            string newVal = newValue as string;
            HostSelector instance = bindable as HostSelector;
            if (instance.HostnameEntry.Text != newVal)
            {
                instance.HostnameEntry.Text = newVal;
            }

        }

        #endregion Hostname property


    }

}
