using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel.Security;
using System.Text;
using System.Windows.Forms;

namespace FCOMMON
{
    public partial class fBase : Form 
    {
        public Boolean UseFormSetting { get; set; }
        public fBase()
        {
            InitializeComponent();
            this.KeyPreview = true;
            UseFormSetting = false;
            this.FormClosed += fBase_FormClosed;
            this.KeyDown += fBase_KeyDown;
        }

        void fBase_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Escape) this.Close();
        }

        void fBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            if(UseFormSetting)
            {
                var form = this as Form;
                FCOMMON.Util.SetFormStatus(ref form, this.Name, false);
            }
        }

        private void fBase_Load(object sender, EventArgs e)
        {
            if(UseFormSetting)
            {
                var form = this as Form;
                FCOMMON.Util.SetFormStatus(ref form, this.Name, true);
            }

            EnsureVisibleAndUsableSize();
        }

        protected void EnsureVisibleAndUsableSize()
        {
            var visibleBounds = Screen.GetWorkingArea(this);
            var isOutOfView = (Left > visibleBounds.Right) || (Top > visibleBounds.Bottom) ||
                (Right < visibleBounds.Left) || (Bottom < visibleBounds.Top);
            if (isOutOfView)
            {
                CenterToScreen();
            }

            var minW = this.MinimumSize.Width;
            var minH = this.MinimumSize.Height;
            if (minW < 10) minW = 10;
            if (minH < 10) minH = 10;
            //var minW = Math.Max(480, this.MinimumSize.Width);
            //var minH = Math.Max(640, this.MinimumSize.Height);

            if (Width < minW || Height < minH)
            {
                this.Size = new Size(minW, minH);
            }
        }

    }
}
