using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Decal.Interop.Controls;
using Decal.Interop.Inject;

namespace Decal.DecalControls
{
    [ComVisible(true)]
    [Guid("E099DC60-0F19-4690-AB7C-8B5834DA286E")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces("Decal.Interop.Controls.IChoiceEvents\0\0")]
    [ProgId("DecalControls.Choice")]
    public class ChoiceImpl : ControlBase, IChoice
    {
        public event IChoiceEvents_DestroyEventHandler Destroy;
        public event IChoiceEvents_ChangeEventHandler Change;
        public event IChoiceEvents_DropDownEventHandler DropDown;

        private struct ChoiceItem
        {
            public string Display;
            public object Data;
        }

        private readonly List<ChoiceItem> _items = new List<ChoiceItem>();
        private int _selected = -1;
        private bool _dropped;
        private int _dropLines = 5;

        public void AddChoice(string strDisplay, object vData = null)
        {
            _items.Add(new ChoiceItem { Display = strDisplay, Data = vData });
        }

        public int ChoiceCount => _items.Count;

        public object Data
        {
            get => (_selected >= 0 && _selected < _items.Count) ? _items[_selected].Data : null;
            set
            {
                if (_selected >= 0 && _selected < _items.Count)
                {
                    var item = _items[_selected];
                    item.Data = value;
                    _items[_selected] = item;
                }
            }
        }

        public string Text
        {
            get => (_selected >= 0 && _selected < _items.Count) ? _items[_selected].Display : "";
            set
            {
                if (_selected >= 0 && _selected < _items.Count)
                {
                    var item = _items[_selected];
                    item.Display = value ?? "";
                    _items[_selected] = item;
                    Invalidate();
                }
            }
        }

        public void RemoveChoice(int nIndex)
        {
            if (nIndex >= 0 && nIndex < _items.Count)
            {
                _items.RemoveAt(nIndex);
                if (_selected >= _items.Count) _selected = _items.Count - 1;
                Invalidate();
            }
        }

        public bool Dropped
        {
            get => _dropped;
            set
            {
                _dropped = value;
                if (_dropped) DropDown?.Invoke(ID);
                Invalidate();
            }
        }

        public int Selected
        {
            get => _selected;
            set
            {
                if (value >= -1 && value < _items.Count && value != _selected)
                {
                    _selected = value;
                    Invalidate();
                    Change?.Invoke(ID, _selected);
                }
            }
        }

        public int DropLines { get => _dropLines; set { _dropLines = value; Invalidate(); } }

        public void Clear()
        {
            _items.Clear();
            _selected = -1;
            Invalidate();
        }

        protected override void OnDestroy() => Destroy?.Invoke(ID);
    }
}
