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

namespace FileTypeRegist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Reset();
        }

        void Reset()
        {
            Reset(true);
        }

        void Reset(bool resetExterndInfo)
        {
            pictureBox1.Image = null;
            pictureBox1.BorderStyle = BorderStyle.FixedSingle;
            textBox_desc.Text = "";
            groupBox1.Enabled = false;

            if (resetExterndInfo)
                textBox_extend.Text = "";
        }

        FileTypeRegister fileReg;

        private void linkLabel_search_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //FileTypeRegister.Refresh();

            Reset(false);
            try
            {
                string extendName = textBox_extend.Text;
                extendName = extendName.TrimStart('.');

                if (string.IsNullOrEmpty(extendName))
                    return;

                fileReg = new FileTypeRegister(extendName);
                textBox_desc.Text = fileReg.FileDescription;

                IconExactor ie = new IconExactor(fileReg.IconInfo.IconFile);
                pictureBox1.Image = ExtractIcon.GetIcon("."+extendName,false).ToBitmap();
                pictureBox1.BorderStyle = BorderStyle.None;

                groupBox1.Enabled = true;
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("查询文件扩展名信息失败！\n请先在windws中设置其默认访问程序。" , ""
                    , MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, ""
                    , MessageBoxButtons.OK, MessageBoxIcon.Warning);
                throw;
            }
            
        }

        private void linkLabel_cancel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Reset();
        }

        private void linkLabel_ok_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string desc = textBox_desc.Text.Trim();
            fileReg.FileDescription = desc;

            if (icoForm.IconInfo != null)
            {
                fileReg.IconInfo = icoForm.IconInfo;
            }

            FileTypeRegister.Refresh();

            MessageBox.Show("设置完成。"
                ,"",MessageBoxButtons.OK,MessageBoxIcon.Information);

            Reset();
        }

        IconForm icoForm = new IconForm();
        private void linkLabel_ico_change_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (icoForm.ShowDialog() != DialogResult.OK)
                return;

            IconExactor ie = new IconExactor(icoForm.IconInfo.IconFile);
            pictureBox1.Image = ie.IconList[icoForm.IconInfo.Index].ToBitmap();
        }
    }
}