using Model;
using Permission;
using PlcCom;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
public class MyUiButton : UISymbolButton,IPermissionControl, IActionableControl
{
    [Category("MyControl")]
    [Description("是否需要用户二次确认")]
    public bool NeedUserEnsure { get; set; }

    [Category("MyControl")]
    [Description("二次确认时的信息提示")]
    [DefaultValue(null)]
    public string EnsureTip { get; set; }

    [Category("MyControl")]
    [Description("控件要求的权限等级")]
    [Browsable(true)]
    [DefaultValue(PermissionLevel.操作工)]
    public PermissionLevel RequiredPermissionLevel { get => requiredPermissionLevel; set { requiredPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= requiredPermissionLevel; HandlePermissionChange(); } }
    private PermissionLevel requiredPermissionLevel = PermissionLevel.操作工;

    [Category("MyControl")]
    [Description("当前登录的权限")]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public PermissionLevel CurrentPermissionLevel { get => currentPermissionLevel; set { currentPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= requiredPermissionLevel; HandlePermissionChange(); } }
    private PermissionLevel currentPermissionLevel = PermissionLevel.无权限;

    [Category("MyControl")]
    [Description("控件是否获得了权限")]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public bool IsPermissionGot { get; private set; }

    protected virtual void HandlePermissionChange()
    {
        this.Enabled = IsPermissionGot;
    }

    public delegate void OptionSelectHandler(object sender, DialogResult dialogResult);

    [Category("MyControl")]
    [Description("二次确认时用户选择事件")]
    [Browsable(true)]
    public event OptionSelectHandler OptionSelect;

    public MyUiButton()
    {
  
    }

    protected sealed override void OnMouseDown(MouseEventArgs e)
    {
        OnCustomMouseDown(e);
        base.OnMouseDown(e);
    }
    protected sealed override void OnMouseUp(MouseEventArgs e)
    {
        OnCustomMouseUp(e);
        base.OnMouseUp(e);
    }

    protected virtual void OnCustomMouseDown(MouseEventArgs e)
    {

    }
    protected virtual void OnCustomMouseUp(MouseEventArgs e)
    {
        
    }
    protected bool IfContinue()
    {
        if (NeedUserEnsure)
        {
            string msg;
            if (string.IsNullOrEmpty(EnsureTip))
            {
                if(string.IsNullOrEmpty(Text))
                {
                    msg = $"是否操作：{Name}？";
                }
                else
                {
                    msg = $"是否操作：{Text}？";
                }
                  
            }
            else
            {
                msg = EnsureTip;
            }
            var dr = MessageBox.Show(msg, "操作确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            OptionSelect?.Invoke(this, dr);
            return dr == DialogResult.Yes;
        }
        else
        {
            return true;
        }
    }



}

