﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using MadsKristensen.EditorExtensions.Settings;
using Microsoft.CSS.Core;
using Microsoft.VisualStudio.Utilities;
using Microsoft.CSS.Core.Checker;
using Microsoft.CSS.Core.Parser;
using Microsoft.CSS.Core.TreeItems.Selectors;

namespace MadsKristensen.EditorExtensions.Css
{
    [Export(typeof(ICssItemChecker))]
    [Name("StarSelectorErrorTagProvider")]
    [Order(After = "Default Declaration")]
    internal class StarSelectorErrorTagProvider : ICssItemChecker
    {
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            SimpleSelector sel = (SimpleSelector)item;

            if (!WESettings.Instance.Css.ValidateStarSelector || !sel.IsValid || context == null)
                return ItemCheckResult.Continue;

            if (sel.Text == "*")
            {
                //string afterStar = sel.Text.Length > index + 1 ? sel.Text.Substring(index + 1) : null;
                //if (afterStar == null || !afterStar.Trim().StartsWith("html", StringComparison.OrdinalIgnoreCase))
                //{
                string errorMessage = string.Format(CultureInfo.InvariantCulture, Resources.PerformanceDontUseStarSelector);

                SimpleErrorTag tag = new SimpleErrorTag(sel, errorMessage);

                context.AddError(tag);
                //}
            }

            return ItemCheckResult.Continue;
        }


        public IEnumerable<Type> ItemTypes
        {
            get { return new[] { typeof(SimpleSelector) }; }
        }
    }
}
