﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Linq;
using Tellurium.Core.Actions;
using Tellurium.Core.CoreObjects;
using Tellurium.Core.Enumerations;
using Tellurium.Core.Interfaces;

namespace Tellurium.Core.Containers
{
    /// <summary>
    /// The Package class.
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public class Package
    {
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        [JsonProperty("name")]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the web browser.
        /// </summary>
        /// <value>
        /// The web browser.
        /// </value>
        [JsonProperty("browser")]
        [JsonConverter(typeof(StringEnumConverter))]
        public WebBrowser WebBrowser { get; set; }

        /// <summary>
        /// Gets or sets the actions.
        /// </summary>
        /// <value>
        /// The actions.
        /// </value>
        [JsonProperty("actions")]
        public List<ActionBase> Actions { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="Package"/> class.
        /// </summary>
        public Package()
        {
            Actions = new List<ActionBase>();
        }

        /// <summary>
        /// Gets all tests.
        /// </summary>
        /// <value>
        /// All tests.
        /// </value>
        public IEnumerable<ITest> AllTests
        {
            get
            {
                return Actions.OfType<TestAction>().Cast<ITest>().ToList();
            }
        }

        /// <summary>
        /// Gets the pending tests.
        /// </summary>
        /// <value>
        /// The pending tests.
        /// </value>
        public IEnumerable<ITest> PendingTests
        {
            get { return AllTests.Where(x => x.GetResult() == TestStatus.Pending); }
        }

        /// <summary>
        /// Gets the passed tests.
        /// </summary>
        /// <value>
        /// The passed tests.
        /// </value>
        public IEnumerable<ITest> PassedTests
        {
            get { return AllTests.Where(x => x.GetResult() == TestStatus.Pass); }
        }

        /// <summary>
        /// Gets the failed tests.
        /// </summary>
        /// <value>
        /// The failed tests.
        /// </value>
        public IEnumerable<ITest> FailedTests
        {
            get { return AllTests.Where(x => x.GetResult() == TestStatus.Fail); }
        }
    }
}
