using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aitex.Core.Common;
using MECF.Framework.Common.DBCore;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Jobs;
using MECF.Framework.Common.SubstrateTrackings;

namespace SicRT.Equipments.Schedulers
{

    class SchedulerDBCallback 
    {
        public void LotCreated(ControlJobInfo cj)
        {
            ModuleName module = ModuleHelper.Converter(cj.Module);
            Guid carrierGuid = CarrierManager.Instance.GetCarrier(cj.Module).InnerId;

            LotDataRecorder.StartLot(cj.LotInnerId.ToString(), carrierGuid.ToString(), "", cj.LotName, cj.Module, cj.Module, cj.LotWafers.Count);

            foreach (var waferInfo in cj.LotWafers)
            {
                LotDataRecorder.InsertLotWafer(cj.LotInnerId.ToString(), waferInfo.WaferInnerID.ToString());

                WaferDataRecorder.SetWaferSequence(waferInfo.WaferInnerID.ToString(), waferInfo.ProcessJobID);
            }
        }


        public void LotFinished(ControlJobInfo cj)
        {

            int unprocessed = 0;
            int aborted = 0;
            foreach (var waferInfo in cj.LotWafers)
            {
                if (waferInfo.ProcessState == WaferProcessStatus.Failed)
                {
                    aborted++;
                    continue;
                }

                if (waferInfo.ProcessState != WaferProcessStatus.Completed)
                {
                    unprocessed++;
                    continue;
                }

            }

            LotDataRecorder.EndLot(cj.LotInnerId.ToString(), aborted, unprocessed);
        }
    }
}
