using System;
using System.Windows.Forms;
using 名单编辑器.Properties;

namespace 名单编辑器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Settings.Default.isediting = false;
            Settings.Default.Save();
        }

        private static int num = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            Settings.Default.isediting = true;
            Settings.Default.Save();
            listView1.BeginUpdate();
            for (int i = 0; i < textBox1.Lines.Length; i++)
            {
                ListViewItem listViewItem = new ListViewItem
                {
                    Text = textBox1.Lines[i]
                };
                if (listViewItem.Text != "" | listViewItem.Text != string.Empty)
                {
                    listView1.Items.Add(listViewItem);
                    num++;
                    保存SToolStripButton.Enabled = true;
                }
            }
            listView1.EndUpdate();
            toolStripStatusLabel2.Text = num.ToString();
        }

        private void listView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 46)
            {
                for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
                {
                    ListViewItem item = listView1.SelectedItems[i];
                    listView1.Items.Remove(item);
                    num--;
                    toolStripStatusLabel2.Text = num.ToString();
                }
            }
        }

        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {
            if (Settings.Default.isediting == true)
            {
                if (num == 0)
                    MessageBox.Show(Resources.NullNumSaveErr, Resources.NullNumSaveErrTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    Program.Save();
                    Settings.Default.isediting = false;
                    Settings.Default.Save();
                }
            }
            else
                MessageBox.Show(Resources.NullSaveErr, Resources.NullSaveErrTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void 新建NToolStripButton_Click(object sender, EventArgs e)
        {
            if (Settings.Default.isediting)
            {
                DialogResult = MessageBox.Show(Resources.NewButEdited, Resources.NewButEditedTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (DialogResult == DialogResult.Yes)
                {
                    MessageBox.Show("Y");
                    Program.Save();
                    Program.New();
                }
                if (DialogResult == DialogResult.No)
                {
                    MessageBox.Show("N");
                }
            }
            else
                Program.New();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
            {
                ListViewItem item = listView1.SelectedItems[i];
                listView1.Items.Remove(item);
                num--;
                toolStripStatusLabel2.Text = num.ToString();
                if (num == 0)
                    toolStripButton1.Enabled = false;
            }
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            剪切UToolStripButton.Enabled = true;
            复制CToolStripButton.Enabled = true;
            粘贴PToolStripButton.Enabled = true;
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            剪切UToolStripButton.Enabled = false;
            复制CToolStripButton.Enabled = false;
            粘贴PToolStripButton.Enabled = false;
        }

        private void listView1_Enter(object sender, EventArgs e)
        {
            if (num != 0)
                toolStripButton1.Enabled = true;
        }

        private void listView1_Leave(object sender, EventArgs e)
        {
            toolStripButton1.Enabled = false;
        }

        private void 复制CToolStripButton_Click(object sender, EventArgs e)
        {
            if(textBox1.SelectionLength == 0)
            {
                textBox1.SelectAll();
            }
            else
            {
                textBox1.Copy();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Settings.Default.isediting)
            {
                if (MessageBox.Show(Resources.ExitContent, Resources.ExitCTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    Environment.Exit(0);
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                Environment.Exit(0);
            }
        }
    }
}