using System;
using System.Collections.Generic;
using System.Linq;

namespace InABox.Core
{
    [Caption("QA Checks")]
    [UserTracking(typeof(DigitalForm))]
    public class QAQuestion : Entity, IRemotable, IPersistent, ISequenceable, IOneToMany<DigitalForm>, ILicense<DigitalFormsLicense>
    {
        public QAQuestion()
        {
            Answer = QAAnswer.Comment;
        }

        [NullEditor]
        [Obsolete("Replaced with Form")]
        public QAFormLink QAForm => InitializeField(ref _qAForm, nameof(QAForm));
        private QAFormLink? _qAForm;

        public DigitalFormLink Form => InitializeField(ref _form, nameof(Form));
        private DigitalFormLink? _form;

        [NullEditor]
        public string Section { get; set; }

        [TextBoxEditor]
        public string Code { get; set; }

        [TextBoxEditor]
        public string Question { get; set; }

        [MemoEditor]
        public string Description { get; set; }

        [EnumLookupEditor(typeof(QAAnswer))]
        public QAAnswer Answer { get; set; }

        [MemoEditor]
        public string Parameters { get; set; }

        //[NullEditor]
        //public String Value { get; set; }

        //[MemoEditor]
        //public String Notes { get; set; }

        [NullEditor]
        public long Sequence { get; set; }

        protected override void DoPropertyChanged(string name, object? before, object? after)
        {
            base.DoPropertyChanged(name, before, after);
            if (name.Equals("Answer"))
            {
                var answer = (QAAnswer)after;

                if (answer == QAAnswer.Choice)
                    Parameters =
                        "Options: Value 1, Value 2, Value 3\n" +
                        "Colors: Green, Yellow, Red\n" +
                        "Default: Value 1\n";
                else if (answer == QAAnswer.Number)
                    Parameters =
                        "Minimum: 0.0\n" +
                        "Maximum: 100.0\n" +
                        "Default: 0.0\n";
                else if (answer == QAAnswer.Text)
                    Parameters =
                        "Default: \n";
                else if (answer == QAAnswer.Combo)
                    Parameters =
                        "Options: Value 1, Value 2, Value 3\n" +
                        "Default: Value 1\n";
                else
                    Parameters = "";
            }
        }

        public Dictionary<string, string> ParseParameters()
        {
            var result = new Dictionary<string, string>();
            var lines = Parameters == null ? new string[] { } : Parameters.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var line in lines)
            {
                var comps = line.Split(':');
                if (comps.Length > 1)
                    result[comps[0]] = string.Join(":", comps.Skip(1));
                else if (comps.Length == 1)
                    result[comps[0]] = "";
            }

            if (Answer == QAAnswer.Choice)
                CheckParams(result, "Options", "Colors", "Default");
            else if (Answer == QAAnswer.Number)
                CheckParams(result, "Minimum", "Maximum", "Default");
            else if (Answer == QAAnswer.Text)
                CheckParams(result, "Default");
            if (Answer == QAAnswer.Combo)
                CheckParams(result, "Options", "Default");


            return result;
        }

        private void CheckParams(Dictionary<string, string> result, params string[] keys)
        {
            foreach (var key in keys)
                if (!result.ContainsKey(key))
                    result[key] = "";
        }
    }
}