using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using EnumsNET;
using AyaNova.Util;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using AyaNova.Models;


namespace AyaNova.Biz
{


    /// <summary>
    /// Handle data seeding and other trial ops
    /// </summary>
    internal class TrialBiz : BizObject, IJobObject
    {
        internal TrialBiz(AyContext dbcontext, long currentUserId, AuthorizationRoles userRoles)
        {
            ct = dbcontext;
            UserId = currentUserId;
            CurrentUserRoles = userRoles;
            BizType = AyaType.TrialSeeder;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////
        //JOB / OPERATIONS
        //
        public async Task HandleJobAsync(OpsJob job)
        {
            //Hand off the particular job to the corresponding processing code
            //NOTE: If this code throws an exception the caller (JobsBiz::ProcessJobsAsync) will automatically set the job to failed and log the exeption so 
            //basically any error condition during job processing should throw up an exception if it can't be handled

            //There might be future other job types so doing it like this for all biz job handlers for now
            switch (job.JobType)
            {
                case JobType.SeedTestData:
                    await ProcessSeedTestDataAsync(job);
                    // ProcessSeedTestData(job);
                    break;
                default:
                    throw new System.ArgumentOutOfRangeException($"TrialBiz.HandleJobAsync -> Invalid job type{job.JobType.ToString()}");
            }
        }


        /// <summary>
        /// Handle the job
        /// </summary>
        /// <param name="job"></param>
        private async Task ProcessSeedTestDataAsync(OpsJob job)
        {//
         //NOTE: If this code throws an exception the caller will automatically set the job to failed and log the exeption so 
         //basically any error condition during job processing should throw up an exception if it can't be handled

            await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running);
            await JobsBiz.LogJobAsync(job.GId, $"Starting...");
            JObject jobData = JObject.Parse(job.JobInfo);
            var seedLevel = (Seeder.Level.SeedLevel)jobData["seedLevel"].Value<int>();
            var timeZoneOffset = jobData["timeZoneOffset"].Value<decimal>();
            var seed = new Util.Seeder();
            await seed.SeedDatabaseAsync(seedLevel, job.GId, timeZoneOffset);
            await JobsBiz.LogJobAsync(job.GId, "Finished.");
            await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Completed);
        }



        //Other job handlers here...



        /////////////////////////////////////////////////////////////////////

    }//eoc


}//eons

