using System.Collections.ObjectModel;
using Telerik.Maui.Controls.Chat;

namespace MauiApp6
{
    public class ChatViewModel : BaseViewModel
    {
        public IList<SimpleChatItem> Items { get; set; }
        //public IList<ChatItem> Items { get; set; }

        public ObservableCollection<string> SuggestedActions { get; set; }



        private Author authorMe;
        public Author AuthorMe
        {
            get => this.authorMe;
            set => SetAndRaisePropertyChanged(ref this.authorMe, value);
        }


        public ChatViewModel()
        {
            this.Items = new ObservableCollection<SimpleChatItem>();
            //this.Items = new ObservableCollection<ChatItem>();
            this.authorMe = new Author() { Name = "Me" };
            this.SuggestedActions = new ObservableCollection<string>();
        }
        public void Load()
        {
            //Normal
            AddTextMessage("Hello!");
            Task.Delay(1000).ConfigureAwait(true);

            //Template Imnportant
            AddTextMessage("Hello Important!", MessageCategory.Important);
            Task.Delay(1000).ConfigureAwait(true);

            AddTextMessage("Using Telerik MAUI?", MessageCategory.Important);
            this.SuggestedActions = new ObservableCollection<string> { "Yes", "No" };
            AddSuggestedActions();

            /* ???
            DatePickerContext context = new DatePickerContext
            {
                MinDate = new DateTime(2024, 1, 1),
                MaxDate = new DateTime(2024, 2, 2),
                SelectedDate = new DateTime(2024, 1, 16)
            };


            PickerItem pickerItem = new PickerItem { Context = context };
            this.Items.Add(new TextMessage { Text = "Select a date" });
            this.Items.Add(pickerItem);

            context.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "SelectedDate")
                {
                    if (context.SelectedDate != null)
                    {
                        this.Items.Remove(pickerItem);
                        this.Items.Add(new TextMessage { Author = this.AuthorMe, Text = "" + context.SelectedDate });
                    }
                }
            };
            */
        }

        private void AddTextMessage(string msg, MessageCategory category = MessageCategory.Normal) //, ChatSuggestions suggestions = null)
        {
            //this.Items.Add(new TextMessage()
            //{
            //    Author = this.authorMe,
            //    Text = msg
            //});

            this.Items.Add(new SimpleChatItem()
            {
                Author = authorMe,
                Text = msg,
                Category = category
            });
        }

        private void AddSuggestedActions() //Author author)
        {
            var suggestedActionsItem = new SuggestedActionsItem();

            #region Suggestion
            var actions = new List<SuggestedAction>();
            foreach (var action in this.SuggestedActions)
            {
                string actionText = action.ToString();
                actions.Add(new SuggestedAction
                {
                    Text = actionText,
                    Command = new Command(() =>
                    {
                        AddTextMessage(actionText);
                    }),
                });
            }
            suggestedActionsItem.Actions = actions;
            #endregion

            //Transform??
            //this.Items.Add(suggestedActionsItem);
        }

    }
}
