using System;
using System.Drawing;
using System.Windows.Forms;

namespace AgencySystem
{
    /// <summary>
    /// Delegate for ShowPage method
    /// </summary>
    /// <param name="page"></param>
    /// <param name="id"></param>
    /// <param name="name"></param>
    /// 

    public delegate void ShowPage(string page, int id, string name);

    public partial class DefaultScreen : Form
    {
        public static ToolStripStatusLabel StatusLabel { get; set; } = new ToolStripStatusLabel();
        public static bool loggedIn { get; set; }

        public delegate void dChangeMenu(string param);
        public dChangeMenu ChangeMenu;

        public delegate void dChangePage(string param);
        public dChangePage ChangePage;

        public DefaultScreen()
        {
            InitializeComponent();
            StatusLabel = toolStripStatusLabel1;
            loggedIn = false;
            ChangeMenu = new dChangeMenu(this.GenerateMenu);
            ChangePage = new dChangePage(this.ChangeShowPage);
            TestDataWithoutDB.testSchemaWithoutDBMark = false;
            GenerateMenu("");
        }

        private void DefaultScreen_Load(object sender, EventArgs e)
        {
            Panel.Controls.Add(HomePage.Instance);
            Panel.Controls.Add(StatusPage.Instance);
            HomePage.Instance.Dock = DockStyle.Fill;
            StatusPage.Instance.Dock = DockStyle.Fill;
            HomePage.Instance.BringToFront();
        }

        /// <summary>
        /// Function which generates menu (ToolStripMenu) based on log in or not
        /// </summary>
        /// <param name="param"></param>
        private void GenerateMenu(string param)
        {
            hotelsToolStripMenuItem.Visible = false;
            entertainmentsToolStripMenuItem.Visible = false;

            if (!loggedIn)
            {
                homeToolStripMenuItem.Visible = true;
                contactToolStripMenuItem.Visible = true;
                countriesToolStripMenuItem.Visible = false;
                destinationsToolStripMenuItem.Visible = false;
                POIToolStripMenuItem.Visible = false;
                restaurantsToolStripMenuItem.Visible = false;
                settingsToolStripMenuItem.Visible = false;
                loginToolStripMenuItem.Visible = true;
                logoutToolStripMenuItem.Visible = false;
            }
            else
            {
                homeToolStripMenuItem.Visible = true;
                contactToolStripMenuItem.Visible = false;
                countriesToolStripMenuItem.Visible = true;
                destinationsToolStripMenuItem.Visible = true;
                POIToolStripMenuItem.Visible = true;
                restaurantsToolStripMenuItem.Visible = true;
                settingsToolStripMenuItem.Visible = true;
                loginToolStripMenuItem.Visible = false;
                logoutToolStripMenuItem.Visible = true;
            }
        }

        /// <summary>
        /// Function which shows status page after specified actions
        /// </summary>
        /// <param name="type"></param>
        /// <param name="heading"></param>
        /// <param name="description"></param>
        public static void ShowStatusPage(int type, string heading, string description)
        {
            System.Drawing.Color color;
            if (type == 1)
            {
                color = System.Drawing.Color.Green;
            }
            else
            {
                color = System.Drawing.Color.Red;
            }
            StatusPage.StatusPageHeadingLabel.Text = heading;
            StatusPage.StatusPageHeadingLabel.ForeColor = color;
            StatusPage.StatusPageDescriptionLabel.Text = description;

            StatusPage.Instance.Dock = DockStyle.Fill;
            StatusPage.Instance.BringToFront();
        }


        /// <summary>
        /// Function which checks user status - logged in or not - and based on that whether page can be displayed
        /// </summary>
        /// <returns></returns>
        private bool checkLoggedIn()
        {
            if (loggedIn == true)
            {
                return true;
            }
            else
            {
                DefaultScreen.ShowStatusPage(2, Properties.Resources.NotLoggedIn, Properties.Resources.YLoggedFirst);
                return false;
            }
        }

        /// <summary>
        /// Function which changes displayed pages
        /// </summary>
        /// <param name="param"></param>
        private void ChangeShowPage(string param)
        {
            //DefaultScreen.ActiveForm.Width = 800;
            //DefaultScreen.ActiveForm.Height = 440;

            switch (param)
            {
                case "EditCountry": CountriesEditPage.Instance.FillData(); ShowEditCountriesPage(); break;
                case "AddCountry": ShowAddCountriesPage(); break;
                case "BackToShowCountries": ShowCountriesPage(); break;

                case "EditDestination": DestinationsEditPage.Instance.FillData(); ShowEditDestinationsPage(); break;
                case "AddDestination": ShowAddDestinationsPage(); break;
                case "BackToShowDestinations": ShowDestinationsPage(); break;

                case "ShowRestaurant": RestaurantsShowRestaurantPage.Instance.FillData(); ShowRestaurantRestaurantsPage(); break;
                case "EditRestaurant": RestaurantsEditPage.Instance.FillData(); ShowEditRestaurantsPage(); break;
                case "AddRestaurant": ShowAddRestaurantsPage(); break;
                case "BackToShowRestaurants": ShowRestaurantsPage(); break;

                case "AddPOI": ShowAddPOIPage(); break;
                case "EditPOI": ShowEditPOIPage(); break;
                case "BackToShowPOI": ShowPOIPage(); break;
            }
        }

        private void homeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Panel.Controls.Contains(HomePage.Instance))
            {
                Panel.Controls.Add(HomePage.Instance);
                HomePage.Instance.Dock = DockStyle.Fill;
                HomePage.Instance.BringToFront();
            }
            else
                HomePage.Instance.BringToFront();
        }


        private void countriesShowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowCountriesPage();
        }

        /// <summary>
        /// Function shows main countries page (Show)
        /// </summary>
        private void ShowCountriesPage()
        {
            if (checkLoggedIn()==true)
            {
                if (!Panel.Controls.Contains(CountriesShowPage.Instance))
                {
                    Panel.Controls.Add(CountriesShowPage.Instance);
                    CountriesShowPage.Instance.Dock = DockStyle.Fill;
                    CountriesShowPage.Instance.BringToFront();
                }
                else
                    CountriesShowPage.Instance.BringToFront();
            }
        }

        /// <summary>
        /// Functions shows edit countries page
        /// </summary>
        private void ShowEditCountriesPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(CountriesEditPage.Instance))
                {
                    Panel.Controls.Add(CountriesEditPage.Instance);
                    CountriesEditPage.Instance.Dock = DockStyle.Fill;
                    CountriesEditPage.Instance.BringToFront();
                }
                else
                    CountriesEditPage.Instance.BringToFront();
            }
        }

        /// <summary>
        /// Function shows add countries page
        /// </summary>
        private void ShowAddCountriesPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(CountriesAddPage.Instance))
                {
                    Panel.Controls.Add(CountriesAddPage.Instance);
                    CountriesAddPage.Instance.Dock = DockStyle.Fill;
                    CountriesAddPage.Instance.BringToFront();
                }
                else
                    CountriesAddPage.Instance.BringToFront();
            }
        }


        private void destinationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowDestinationsPage();
        }
        /// <summary>
        /// Function shows main destinations page (Show)
        /// </summary>
        private void ShowDestinationsPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(DestinationsShowPage.Instance))
                {
                    Panel.Controls.Add(DestinationsShowPage.Instance);
                    DestinationsShowPage.Instance.Dock = DockStyle.Fill;
                    DestinationsShowPage.Instance.BringToFront();
                }
                else
                    DestinationsShowPage.Instance.BringToFront();
            }
        }

        /// <summary>
        /// Function shows edit destination page
        /// </summary>
        private void ShowEditDestinationsPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(DestinationsEditPage.Instance))
                {
                    Panel.Controls.Add(DestinationsEditPage.Instance);
                    DestinationsEditPage.Instance.Dock = DockStyle.Fill;
                    DestinationsEditPage.Instance.BringToFront();
                }
                else
                    DestinationsEditPage.Instance.BringToFront();
            }
        }

        /// <summary>
        /// Function shows add destination page
        /// </summary>
        private void ShowAddDestinationsPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(DestinationsAddPage.Instance))
                {
                    Panel.Controls.Add(DestinationsAddPage.Instance);
                    DestinationsAddPage.Instance.Dock = DockStyle.Fill;
                    DestinationsAddPage.Instance.BringToFront();
                }
                else
                    DestinationsAddPage.Instance.BringToFront();
            }
        }

        private void POIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowPOIPage();
        }
        /// <summary>
        /// Function shows main restaurants page (Show)
        /// </summary>
        private void ShowPOIPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(POIShowPage.Instance))
                {
                    Panel.Controls.Add(POIShowPage.Instance);
                    POIShowPage.Instance.Dock = DockStyle.Fill;
                    POIShowPage.Instance.BringToFront();
                }
                else
                    POIShowPage.Instance.BringToFront();
            }
        }
        /// <summary>
        /// Function shows add POI page
        /// </summary>
        private void ShowAddPOIPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(POIAddPage.Instance))
                {
                    Panel.Controls.Add(POIAddPage.Instance);
                    POIAddPage.Instance.Dock = DockStyle.Fill;
                    POIAddPage.Instance.BringToFront();
                }
                else
                    POIAddPage.Instance.BringToFront();
            }
        }
        /// <summary>
        /// Function shows edit restaurant page
        /// </summary>
        private void ShowEditPOIPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(POIEditPage.Instance))
                {
                    Panel.Controls.Add(POIEditPage.Instance);
                    POIEditPage.Instance.Dock = DockStyle.Fill;
                    POIEditPage.Instance.BringToFront();
                }
                else
                    POIEditPage.Instance.BringToFront();
            }
        }



        private void restaurantsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowRestaurantsPage();
        }
        /// <summary>
        /// Function shows main restaurants page (Show)
        /// </summary>
        private void ShowRestaurantsPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(RestaurantsShowPage.Instance))
                {
                    Panel.Controls.Add(RestaurantsShowPage.Instance);
                    RestaurantsShowPage.Instance.Dock = DockStyle.Fill;
                    RestaurantsShowPage.Instance.BringToFront();
                }
                else
                    RestaurantsShowPage.Instance.BringToFront();
            }
        }

        /// <summary>
        /// Function shows edit restaurant page
        /// </summary>
        private void ShowEditRestaurantsPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(RestaurantsEditPage.Instance))
                {
                    Panel.Controls.Add(RestaurantsEditPage.Instance);
                    RestaurantsEditPage.Instance.Dock = DockStyle.Fill;
                    RestaurantsEditPage.Instance.BringToFront();
                }
                else
                    RestaurantsEditPage.Instance.BringToFront();
            }
        }

        /// <summary>
        /// Functions shows restaurant page with full details (ShowRestaurant)
        /// </summary>
        private void ShowRestaurantRestaurantsPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(RestaurantsShowRestaurantPage.Instance))
                {
                    Panel.Controls.Add(RestaurantsShowRestaurantPage.Instance);
                    RestaurantsShowRestaurantPage.Instance.Dock = DockStyle.Fill;
                    RestaurantsShowRestaurantPage.Instance.BringToFront();
                }
                else
                    RestaurantsShowRestaurantPage.Instance.BringToFront();
            }
        }

        /// <summary>
        /// Function shows add restaurant page
        /// </summary>
       private void ShowAddRestaurantsPage()
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(RestaurantsAddPage.Instance))
                {
                    Panel.Controls.Add(RestaurantsAddPage.Instance);
                    RestaurantsAddPage.Instance.Dock = DockStyle.Fill;
                    RestaurantsAddPage.Instance.BringToFront();
                }
                else
                    RestaurantsAddPage.Instance.BringToFront();
            }
        }


        private void contactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Panel.Controls.Contains(ContactPage.Instance))
            {
                Panel.Controls.Add(ContactPage.Instance);
                ContactPage.Instance.Dock = DockStyle.Fill;
                ContactPage.Instance.BringToFront();
            }
            else
                ContactPage.Instance.BringToFront();
        }


        private void testingPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(TestingPage.Instance))
                {
                    Panel.Controls.Add(TestingPage.Instance);
                    TestingPage.Instance.Dock = DockStyle.Fill;
                    TestingPage.Instance.BringToFront();
                }
                else
                    TestingPage.Instance.BringToFront();
            }
        }


        private void createNewUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (checkLoggedIn() == true)
            {
                if (!Panel.Controls.Contains(RegistrationNewUser.Instance))
                {
                    Panel.Controls.Add(RegistrationNewUser.Instance);
                    RegistrationNewUser.Instance.Dock = DockStyle.Fill;
                    RegistrationNewUser.Instance.BringToFront();
                }
                else
                    RegistrationNewUser.Instance.BringToFront();
            }
        }


        private void logInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Panel.Controls.Contains(LoginPage.Instance))
            {
                Panel.Controls.Add(LoginPage.Instance);
                LoginPage.Instance.Dock = DockStyle.Fill;
                LoginPage.Instance.BringToFront();
            }
            else
                LoginPage.Instance.BringToFront();
        }

        private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (loggedIn == true)
            {
                loggedIn = false;
                DefaultScreen.ShowStatusPage(1, Properties.Resources.SuccessfullyLoggedOut, Properties.Resources.YSuccessfullyLoggedOut);
                DefaultScreen DefaultScreenForm = (DefaultScreen)Application.OpenForms["DefaultScreen"];
                DefaultScreenForm.ChangeMenu.Invoke("");
            }
        }








        /// <summary>
        /// Methods for design initializations - Page heading, Page panel, Page add button
        /// </summary>
        /// <param name="label"></param>
        static public void initializeHeading(Label label)
        {
            label.Left = 30;
            label.Top = 20;
            label.Font = new Font("Microsoft Sans Serif", 15);
        }
        static public void initializePanel(Panel panel)
        {
            panel.Left = 30;
            panel.Top = 50;
            panel.AutoSize = true;
            panel.Height = 350;
            panel.Width = 720;
        }
        static public void initializeAddButton(Button button)
        {
            button.Left = 600;
            button.Top = 20;
            button.Width = 150;
            button.Height = 30;
            button.BackColor = Color.LightYellow;
            button.Cursor = Cursors.Hand;
        }


        /// <summary>
        /// Thread for real DateTime in right bottom bar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void realTimer_Tick(object sender, EventArgs e)
        {
            dateTimeToolStrip.Text = DateTime.Now.ToString();
        }
    }
}
