using Common;
using Common.Models;
using CommunityToolkit.Mvvm.Messaging;
using LightInject;
using MegaRobo.C00225155.AppServer.ExecuteWorks;
using MegaRobo.C00225155.ControlDevices;
using MegaRobo.C00225155.DataAccess;
using MegaRobo.C00225155.Entities;
using MegaRobo.Contract;
using MegaRobo.Contract.Abstractions;
using MegaRobo.ControlDevices.Abstractions;
using MegaRobo.Logger;
using MegaRobo.RRQuartz;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Net;
using System.Threading.Tasks;

namespace MegaRobo.C00225155.AppServer;

/// <summary>
/// 这里面管理所有的StationServices
/// </summary>
public partial class WorkService : WorkServiceBase
{
    // 当ProjectPro变更时触发的事件
    public event Action ProjectProChanged;
    
    public event EventHandler TickEvent;

    private AppConfigService _appConfigService;
    //private IDataAccessService _dataAccessService;

    /// <summary>
    /// 工站集合
    /// </summary>
    public IList<StationServiceBase> stationList;

    /// <summary>
    /// 投料站 
    /// </summary>
    public StationService_MaterialDosing stationService_Dose;

    /// <summary>
    /// 反应站
    /// </summary>
    public StationService_ReactionProcessing stationService_React;


    private ProjectProperty _projectPro = new ProjectProperty();
    public ProjectProperty ProjectPro
    {
        get { return _projectPro; }
        set
        {
            if (_projectPro != value)
            {
                _projectPro = value;
                ProjectProChanged?.Invoke(); //触发事件
            }
        }
    }

    public override void Initialize(ICoreService coreService, params object[] args)
    {
        this.CoreService = coreService;
        base.Initialize(coreService, args);

        #region webapi相关
        HttpListenerProvider _httpListenerProvider = this.CoreService.GetServiceInstance<HttpListenerProvider>();
        this._httpListenerService = _httpListenerProvider.GetOrAdd(this._appConfigService.ProjectInfo.Code, null);
        this.RegiondRoute("A1");
        this.AddSwaggerSupport();
        #endregion

        stationService_Dose = new StationService_MaterialDosing(CoreService, this, DeviceNames.Plc1, "Station_Dose");
        stationService_Dose.Initialize(this.CoreService);

        //stationService_React = new StationService_ReactionProcessing(CoreService, this, DeviceNames.Plc2, "Station_React");
        //stationService_React.Initialize(this.CoreService);
    }

    protected override void LoadServices()
    {
        this._appConfigService = this.CoreService.GetServiceInstance<AppConfigService>();
        //this._dataAccessService = this.CoreService.GetServiceInstance<IDataAccessService>();
    }

    protected override void LoadDatas()
    {
        base.LoadDatas();
    }

    protected override void AttachEvents()
    {
        base.AttachEvents();
    }
}