using System.Windows.Forms;
using CefSharpWrap;
using CefSharpWrap.AceEditorWrap;

namespace MarkdownRenderer
{
    public partial class StyleSheetForm : Form
    {
        private readonly CefSharpAceEditorWrap _editor;
        public StyleSheetForm()
        {
            InitializeComponent();
            _editor = new CefSharpAceEditorWrap();
            _editor.Mode = AceEditorMode.Css;
            _editor.FillToControl(EditorPanel);

            MainImageList.Images.Add(Icon);

            foreach (var sheetEntry in StyleSheetStore.GetEntries())
            {
                MainListView.Items.Add(new ListViewItem(sheetEntry.Name)
                {
                    ImageIndex = 0,
                    StateImageIndex = -1,
                    Tag = sheetEntry,
                });
            }

            foreach (var sheetEntry in StyleSheetStore.BuiltInEntries)
            {
                MainListView.Items.Add(new ListViewItem(sheetEntry.Name)
                {
                    ImageIndex = 0,
                    StateImageIndex = 0,
                    Tag = sheetEntry,
                });
            }

            //TODO: Implement style sheet manage
        }

        private void NewButton_Click(object sender, System.EventArgs e)
        {
        }

        private void StyleSheetForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //TODO: Save to config store
        }

        private void AllTextBox_TextChanged(object sender, System.EventArgs e)
        {
            //TODO: Active save button
        }

        private void MainListView_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //TODO: No selected, chear all input, disable grid
            //TODO: Bulit-in entry, readonly all input, disable save & delete
            //TODO: Custom entry, normal all input, disable save, enable delete

            if (0 != MainListView.SelectedItems.Count)
            {
                var lvi = MainListView.SelectedItems[0];
                var entry = (StyleSheetEntry)lvi.Tag;
                NameTextBox.Text = entry.Name;
                BodyClassTextBox.Text = entry.BodyClass;
                RemarkTextBox.Text = entry.Remark;
                _editor.Text = entry.Css;
            }
        }

        private void CloseButton_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        private void DragLabel_MouseDown(object sender, MouseEventArgs e)
        {
            //TODO: check name and content, Start drag action with virtual file DataObject
        }

        private void DragLabel_DragEnter(object sender, DragEventArgs e)
        {
        }

        private void DragLabel_DragDrop(object sender, DragEventArgs e)
        {
        }

        private void F12Button_Click(object sender, System.EventArgs e)
        {
            _editor.ShowF12();
        }
    }
}