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

namespace Azyobuzi.Twirunrun
{
    public partial class UpdateStatusTextBox : UserControl
    {
        public UpdateStatusTextBox()
        {
            InitializeComponent();
            Settings.Instance.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == "AtHashtagList")
                {
                    listBox1.Invoke((MethodInvoker)(() =>
                    {
                        var selected = listBox1.SelectedItem;
                        listBox1.Items.Clear();
                        listBox1.Items.AddRange(Settings.Instance.AtHashtagList);
                        listBox1.SelectedItem = selected;
                    }));
                }
            };
        }

        public void Clear()
        {
            textBox1.Clear();
            splitContainer1.Panel2Collapsed = true;
        }

        [Browsable(true)]
        public override string Text
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//読みにくくて辛い
        {
            if (e.KeyChar == (char)Keys.Back)
            {
                if (textBox1.SelectionStart > 0 && "@＠#＃".Contains(textBox1.Text[textBox1.SelectionStart - 1]))
                {
                    splitContainer1.Panel2Collapsed = true;
                }
                return;
            }
            else if (e.KeyChar == (char)Keys.Escape)
            {
                splitContainer1.Panel2Collapsed = true;
                return;
            }
            else if (e.KeyChar == (char)Keys.Enter)
            {
                splitContainer1.Panel2Collapsed = true;
                if ((Control.ModifierKeys & Keys.Shift) != Keys.Shift && listBox1.SelectedIndex != -1)
                {
                    int atHashtagStart = textBox1.Text.LastIndexOfAny("@＠#＃".ToArray(), textBox1.SelectionStart - 1);
                    var selected = listBox1.SelectedItem.ToString();
                    textBox1.Text = textBox1.Text.Substring(0, atHashtagStart) + selected + textBox1.Text.Substring(textBox1.SelectionStart);
                    textBox1.SelectionStart = atHashtagStart + selected.Length;
                    e.Handled = true;
                }
                return;
            }

            if ("@＠#＃".Contains(e.KeyChar))
            {
                if (Settings.Instance.AutoComplete)
                    splitContainer1.Panel2Collapsed = false;
                return;
            }

            if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ_-0123456789".Contains(e.KeyChar))
                splitContainer1.Panel2Collapsed = true;
        }

        public int TextLength
        {
            get
            {
                return textBox1.TextLength;
            }
        }

        public void Select(int start, int length)
        {
            textBox1.Select(start, length);
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!splitContainer1.Panel2Collapsed)
            {
                if (e.KeyCode == Keys.Down)
                    listBox1.SelectedIndex++;
                else if (e.KeyCode == Keys.Up)
                    listBox1.SelectedIndex--;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (!splitContainer1.Panel2Collapsed)
            {
                int atHashtagStart = textBox1.Text.LastIndexOfAny("@＠#＃".ToArray(), textBox1.SelectionStart - 1);
                listBox1.SelectedItem = Settings.Instance.AtHashtagList
                    .FirstOrDefault(_ =>
                        _.StartsWith(
                            textBox1.Text.Substring(atHashtagStart, textBox1.SelectionStart - atHashtagStart)
                        )
                    )
                    ?? listBox1.SelectedItem;
            }
        }
    }
}
