using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Project
{
    public partial class fMain
    {
        /// <summary>
        /// 웨이퍼를 집기 위해 지정된 위치로 이동을 합니다.
        /// </summary>
        /// <returns></returns>
        private bool _SM_RUN_INIT(Boolean firstRun, TimeSpan RunTime)
        {
            //result message
            int progress = 0;
            var ProgressMax = 13;

            //setting socket            
            UpdateProgressStatus(++progress, ProgressMax, "소켓 초기화"); System.Threading.Thread.Sleep(5);
            if (PUB.sock_debug.Listen(PUB.setting.listenPort))
                PUB.log.AddI("Socket Listen : " + PUB.setting.listenPort);
            else
                PUB.log.AddE("Socket Listen  : " + PUB.sock_debug.errorMessage);


            //UpdateProgressStatus(++progress, ProgressMax, "PLC 확인"); System.Threading.Thread.Sleep(5);
            //if (PUB.setting.Port_PLC != "")
            //{
            //    if (PUB.PLC.Init(PUB.setting.Port_PLC, PUB.setting.Baud_PLC))
            //    {
            //        //VAR.BOOL[eVarBool.FLAG_SETUP] = true;
            //        PUB.log.Add("Entering Setup Mode (Main)");
            //    }
            //    else PUB.log.AddE("PLC Init error : " + PUB.PLC.GetErrorMessage());
            //}

            //UpdateProgressStatus("PLC #2 확인", ++progress, ProgressMax); System.Threading.Thread.Sleep(5);
            //if (Pub.setting.Port_PLC2 != "")
            //{
            //    if (Pub.plcS.Init(Pub.setting.Port_PLC2, Pub.setting.Baud_plc2))
            //    {
            //        Pub.plcS.SetFlag(Device.PLC2.PLCFlag.FLAG_SETUP, true);
            //        Pub.log.Add("Entering Setup Mode (Sub)");
            //    }
            //    else Pub.log.AddE("DIO #S Init error : " + Pub.plcS.GetErrorMessage());
            //}

            //setting Port_Xbee
            //UpdateProgressStatus(++progress, ProgressMax, "Port_Xbee 확인"); System.Threading.Thread.Sleep(5);
            //if (PUB.setting.Port_XBE.isEmpty() == false)
            //{
            //    PUB.XBE.PortName = PUB.setting.Port_XBE;
            //    if (PUB.XBE.Open() == false)
            //        PUB.log.AddE(string.Format("Barcode Port({0}) Open Error: {1}", PUB.setting.Port_XBE, PUB.XBE.errorMessage));
            //}
            //else
            //{
            //    PUB.log.AddAT("NOT-SET Port_Xbee PORTNAME");
            //    PUB.XBE.errorMessage = "NOT-SET Port_Xbee PORTNAME";
            //}

            ////setting Port_Xbee
            //UpdateProgressStatus(++progress, ProgressMax, "Port_AGV 확인"); System.Threading.Thread.Sleep(5);
            //if (PUB.setting.Port_AGV.isEmpty() == false)
            //{
            //    PUB.AGV.PortName = PUB.setting.Port_AGV;
            //    PUB.AGV.BaudRate = PUB.setting.Baud_AGV;
            //    if (PUB.AGV.Open() == false)
            //        PUB.log.AddE(string.Format("RFID Host:{0} Open Error: {1}", PUB.setting.Port_AGV, PUB.AGV.errorMessage));
            //}
            //else
            //{
            //    PUB.log.AddAT("NOT-SET Port_AGV PORTNAME");
            //    PUB.AGV.errorMessage = "NOT-SET Port_AGV PORTNAME";
            //}


            ////setting Port_Xbee
            //UpdateProgressStatus(++progress, ProgressMax, "Port_BMS 확인"); System.Threading.Thread.Sleep(5);
            //if (!PUB.setting.Port_BAT.isEmpty())
            //{
            //    PUB.BAT.PortName = PUB.setting.Port_BAT;//.Port_BMS;
            //    PUB.BAT.BaudRate = PUB.setting.Baud_BAT;//
            //    PUB.BAT.ScanInterval = 1000;// Pub.setting.interval_bms;
            //    var binit_bms = PUB.BAT.Open();
            //    if (binit_bms == false) PUB.log.AddE(string.Format("binit_bms Port({0}) Open Error: {1}", PUB.setting.Port_BAT, PUB.BAT.errorMessage));
            //}
            //else
            //{
            //    PUB.log.AddAT("NOT-SET binit_bms PORTNAME");
            //}

            //남은 공간
            UpdateProgressStatus(++progress, ProgressMax, "공간 확인"); System.Threading.Thread.Sleep(5);
            CheckFreeSpace(); //181225

            //시스템 오류가 있다면 경고 메시지를 표싷ㄴ다.
            UpdateProgressStatus(++progress, ProgressMax, "오류 수집"); System.Threading.Thread.Sleep(5);
            //var AlertMessage = new System.Text.StringBuilder();
            //if (Pub.Xbee.IsInit == false) AlertMessage.AppendLine(string.Format("BARCODE : {0}", Pub.Xbee.errorMessage));
            if (freeSpaceRate < PUB.setting.AutoDeleteThreshold)
            {
                PUB.Speak(string.Format("디스크 용량이 {0:N0}% 남았습니다", freeSpaceRate));
            }

            //if (AlertMessage.Length > 1)
            //{
            //    AlertMessage.Insert(0, "SYSTEM ALERT\n");
            //    Pub.popup.setMessage(AlertMessage.ToString());
            //}

            UpdateProgressStatus(++progress, ProgressMax, "--"); System.Threading.Thread.Sleep(5);
            PUB.log.Add("초기화 완료");
            return true;
        }
    }
}
