using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace GcDevicePc
{
    public delegate void SaveIniFile(string filename);
    public delegate void EnableFWin();

    public partial class SaveCfg : Form
    {
        public event SaveIniFile SaveInI;
        public event EnableFWin EnableFWindow;

        public SaveCfg()
        {
            InitializeComponent();
        }


        bool VerifyName()
        {
            string strtmp;
            DirectoryInfo MethodDir = new DirectoryInfo(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder);
            foreach (FileInfo file in MethodDir.GetFiles("*.ini"))
            {
                strtmp = this.newcfgname.Text + ".ini";
                if (strtmp == file.Name)
                {
                    return false;
                }
            }
            return true;
        }

        private void newcfgname_TextChanged(object sender, EventArgs e)
        {
            if (VerifyName())
            {
                this.promptlabel.Text = "文件名可用";
                this.promptlabel.ForeColor = Color.Green;
                this.prompt.Text = "";
            }
            else
            {
                this.promptlabel.Text = "文件名存在";
                this.promptlabel.ForeColor = Color.Red;
                this.prompt.Text = "ps:如文件存在，会更新原有状态";
            }

            if (this.newcfgname.Text == "")
            {
                this.promptlabel.Text = "";
            }
        }

        private void okbutton_Click(object sender, EventArgs e)
        {
            string filename = this.newcfgname.Text +".ini";
            filename = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder, filename);
            SaveInI(filename);
            this.Close();
        }

        private void exitbutton_Click(object sender, EventArgs e)
        {
            EnableFWindow();
            this.Close();
        }
    }
}
