using System.Linq;
using Abp.Extensions;
using Microsoft.EntityFrameworkCore;
using YGNT.QuestionLibrary.Core.BaseData;
using YGNT.QuestionLibrary.Core.BaseData.Enum;

namespace YGNT.QuestionLibrary.EntityFrameworkCore.Seed
{
    public class QuestionTypeCreator
    {
        private readonly QuestionLibraryDbContext _context;

        private readonly object[,] _types =
        {
            { "单选题",QuestionCategory.客观题},
            { "多选题",QuestionCategory.客观题},
            { "判断题",QuestionCategory.判断题},
            { "简答题",QuestionCategory.非客观题}
        };

        public QuestionTypeCreator(QuestionLibraryDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            for (var i = 0; i < _types.GetLength(0); i++)
            {
                var isExit = _context.QuestionTypes.Any(x => x.Name == _types[i, 0].ToString());
                if (!isExit)
                {
                    var lastSort = _context.QuestionTypes.Select(x => x.Sort)
                        .OrderByDescending(x => x)
                        .FirstOrDefault();
                    _context.QuestionTypes.Add(new QuestionType()
                    {
                        IsStruct = true,
                        Name = _types[i, 0].ToString(),
                        QuestionCategory = _types[i, 1].To<QuestionCategory>(),
                        Sort = lastSort + 1
                    });
                    _context.SaveChanges();
                }
            }
        }
    }
}