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

namespace ryControls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MsgLink : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public MsgLink()
        {
            InitializeComponent();
            timer1.Enabled = true;
            Clear();
        }
        int _MaxItemHeight = 0;
        /// <summary>
        /// 最大高度
        /// </summary>
        [Category("Appearance"), Description("最大高度")]
        public int MaxItemHeight
        {
            get { return _MaxItemHeight; }
            set { _MaxItemHeight = value; pnlContent.MaximumSize = new Size(10000, _MaxItemHeight); }
        }
        private string _title = "";
        /// <summary>
        /// 标题
        /// </summary>
        [Category("内容"), Description("标题")]
        public string Title
        {
            get { return _title; }
            set { if (ShowDateTime) { lblTitle.Text = value+"("+ ryControls.Pram.Pram.DateDiff(AddTime, DateTime.Now)+")"; } else { lblTitle.Text = value; } _title = value; }
        }
        /// <summary>
        /// 是否显示时间
        /// </summary>
        public bool ShowDateTime { get; set; } = false;
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime AddTime { get; set; } = DateTime.Now;
        int iTop = 3;
        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            pnlContent.Controls.Clear();
            Height = lblTitle.Height;
            pnlContent.Height = 0;
            iTop = 6;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="url"></param>
        public void AddItem(string title,string url)
        {
            LinkLabel item = new LinkLabel();
            item.LinkClicked += new LinkLabelLinkClickedEventHandler(Item_LinkClicked);
            item.Text = title;
            item.ContextMenuStrip = contextMenuStrip1;
            item.AutoSize = false;
            item.Tag= url;
            item.Top = iTop;
            item.Left = 3;
            item.Width = pnlContent.Width-20;
            toolTip1.SetToolTip(item, title);
            item.Anchor =AnchorStyles.Left | AnchorStyles.Right |AnchorStyles.Top;
            //item.Height = 123;
            pnlContent.Controls.Add(item);
            iTop += item.Height+2;
        }

        void Item_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    LinkLabel item = (LinkLabel)sender;
                    System.Diagnostics.Process.Start(item.Tag.ToString());
                    e.Link.Visited = true;
                }
            }
            catch { }
        }

        private void 打开页面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                LinkLabel lnk = (LinkLabel)contextMenuStrip1.SourceControl;
                System.Diagnostics.Process.Start(lnk.Tag.ToString());
                lnk.LinkVisited = true;
            }
            catch { }
        }

        private void 复制文本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                LinkLabel lnk = (LinkLabel)contextMenuStrip1.SourceControl;
                Clipboard.SetText(lnk.Text);
            }
            catch { }
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (ShowDateTime) { lblTitle.Text = _title + "(" + ryControls.Pram.Pram.DateDiff(AddTime, DateTime.Now) + ")"; }
            else
            { lblTitle.Text = _title; }
        }
    }
}
