using System;
using FastReport.Controls;
using FastReport.Design;
using FastReport.Forms;
using FastReport.Utils;

using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;


namespace FastReport.Auth
{
    public partial class AuthForm : BaseDialogForm
    {
        private readonly List<DesignerOptionsPage> optionsPages;
        private PageControl pageControl1;


        private void AddPages(DesignerOptionsPage page)
        {
            if (page != null)
            {
                foreach (TabPage tab in page.tc1.TabPages)
                {
                    PageControlPage panel = new PageControlPage();
                    panel.Text = tab.Text;
                    panel.Dock = DockStyle.Fill;
                    panel.BackColor = SystemColors.Window;
                    while (tab.Controls.Count > 0)
                    {
                        var control = tab.Controls[0];
                        control.Parent = panel;
                    }

                    pageControl1.Controls.Add(panel);
                }

                optionsPages.Add(page);
                page.Init();
            }
        }

        public AuthForm()
        {
            optionsPages = new List<DesignerOptionsPage>();
            InitializeComponent();
            Localize();

            // add default pages
            AddPages(new AuthFormOptions());

            // Load active options tab.
            int activePageIndex;
            XmlItem options = Config.Root.FindItem("Designer").FindItem("OptionsWindow");
            int.TryParse(options.GetProp("ActiveTab"), out activePageIndex);
            pageControl1.ActivePageIndex = activePageIndex;
            UIUtils.CheckRTL(this);
            UpdateDpiDependencies();

        }

        private void DesignerOptions_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (DesignerOptionsPage page in optionsPages)
            {
                page.Done(DialogResult);
            }

            // Save active options tab.
            XmlItem options = Config.Root.FindItem("Designer").FindItem("OptionsWindow");
            options.SetProp("ActiveTab", pageControl1.ActivePageIndex.ToString());
        }


        public override void Localize()
        {
            base.Localize();
            Text = Res.Get("Designer,Menu,Help,Account");
        }

        private void InitializeComponent()
        {
            this.pageControl1 = new FastReport.Controls.PageControl();
            this.SuspendLayout();
            // 
            // btnOk
            // 
            this.btnOk.Location = new System.Drawing.Point(364, 276);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(444, 276);
            // 
            // pageControl1
            // 
            this.pageControl1.Location = new System.Drawing.Point(12, 12);
            this.pageControl1.Name = "pageControl1";
            this.pageControl1.SelectorWidth = 139;
            this.pageControl1.Size = new System.Drawing.Size(508, 252);
            this.pageControl1.TabIndex = 1;
            this.pageControl1.Text = "pageControl1";
            // 
            // AuthForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.ClientSize = new System.Drawing.Size(531, 310);
            this.Controls.Add(this.pageControl1);
            this.Name = "AuthForm";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DesignerOptions_FormClosing);
            this.Controls.SetChildIndex(this.btnOk, 0);
            this.Controls.SetChildIndex(this.btnCancel, 0);
            this.Controls.SetChildIndex(this.pageControl1, 0);
            this.ResumeLayout(false);

        }
        public override void UpdateDpiDependencies()
        {
            base.UpdateDpiDependencies();
            foreach (DesignerOptionsPage page in optionsPages)
            {
                page.UpdateDpiDependencies();
                AuthFormOptions temp = page as AuthFormOptions;
                if (temp != null)
                    temp.UpdateCaptionFont(NewDpi);
            }
        }

    }
}
