using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;

namespace Aitex.Core.RT.SCCore
{
    public class SCValue
    {
        public SystemConfigValue System { get; set; }
 
        private Dictionary<string, Tuple<object, PropertyInfo>> _fieldMap =
            new Dictionary<string, Tuple<object, PropertyInfo>>();

        public SCValue()
        {
             System = new SystemConfigValue();
        }

        public List<string> GetKeys()
        {
            return _fieldMap.Keys.ToList();
        }

        public void SetKeys(List<string> keys)
        {
            _fieldMap.Clear();
            Dictionary<string, object> items = new Dictionary<string, object>();
            PropertyInfo[] property = typeof(SCValue).GetProperties();

            foreach (PropertyInfo fiGroup in property)
            {
                object objGroup = fiGroup.GetValue(this, null);

                foreach (PropertyInfo fiItem in objGroup.GetType().GetProperties())
                {
                    string name = String.Format("{0}_{1}", fiGroup.Name, fiItem.Name);

                    if (keys.Contains(name))
                    {
                        _fieldMap[name] = Tuple.Create(objGroup, fiItem);
                    }
                }
            }
        }


        public void Update(Dictionary<string, object> result)
        {
            if (result == null)
                return;

            foreach (KeyValuePair<string, object> item in result)
            {
                if (_fieldMap.ContainsKey(item.Key))
                {
                    _fieldMap[item.Key].Item2.SetValue(_fieldMap[item.Key].Item1, item.Value, null);
                }
            }
        }

        public void Update(string key, string value)
        {

            if (!_fieldMap.ContainsKey(key))
                return;

            if (_fieldMap[key].Item2.PropertyType == typeof(double))
            {
                _fieldMap[key].Item2.SetValue(_fieldMap[key].Item1, Convert.ToDouble(value), null);
            }else if (_fieldMap[key].Item2.PropertyType == typeof(int))
            {
                _fieldMap[key].Item2.SetValue(_fieldMap[key].Item1, Convert.ToInt32(value), null);
            }else if (_fieldMap[key].Item2.PropertyType == typeof(string))
            {
                _fieldMap[key].Item2.SetValue(_fieldMap[key].Item1, value, null);
            
            }else if (_fieldMap[key].Item2.PropertyType == typeof(bool))
            {
                _fieldMap[key].Item2.SetValue(_fieldMap[key].Item1, Convert.ToBoolean(value), null);
            }  

        }

        public Dictionary<string, object> GetValue()
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            foreach (var item in _fieldMap)
            {
                result[item.Key] = item.Value.Item2.GetValue(item.Value.Item1, null);
 
            }
            return result;
        }

        public class SystemConfigValue
        {
            public int  Language { get; set; }
            public bool IsSimulatorMode { get; set; }
            public int TotalWaferCount { get; set; }

            public bool Blade1Enable { get; set; }
            public bool Blade2Enable { get; set; }

            public int  RobotSpeed { get; set; }

            public int  TimeLimitRobotCommand { get; set; }
            public int  TimeLimitForPickWafer { get; set; }
            public int  TimeLimitForPlaceWafer { get; set; }
            public int  TimeLimitForExchangeWafer { get; set; }
            public int  TimeLimitForAlignWafer { get; set; }
  
            public int  TimeLimitForWID { get; set; }

            public int TimeLimitRobotHome { get; set; }

            public int TimeLimitAlignerHome { get; set; }

            public int TimeLimitLoadportHome { get; set; }
 
            public int  TimeLimitLoadportLoad { get; set; }
            public int  TimeLimitLoadportUnload { get; set; }
            public int  TimeLimitLoadportClamp { get; set; }

            public int TimeLimitReadRFID { get; set; }
            public int TimeLimitWriteRFID { get; set; }

            public int RobotCommunicationToken { get; set; }

            public bool DualBlade1TransferEnable { get; set; }

            public int AlignerCommunicationToken { get; set; }

            public bool EnableReadLaserMarker1 { get; set; }
            public bool EnableReadLaserMarker2 { get; set; }

            public double AlignerReadAngle { get; set; }
            public double AlignerDefaultAngle { get; set; }

 
            public string ReaderJob1Main { get; set; }
            public string ReaderJob1Slave { get; set; }
            public string ReaderJob2Main { get; set; }
            public string ReaderJob2Slave { get; set; }

            public bool EnableAutoLockWhenCassetteArrived { get; set; }
            public bool EnableAutoCarrierIdReadAfterLatch { get; set; }
            public bool EnableAutoDockAfterIdRead { get; set; }
            public bool EnableAutoMappingAfterDock { get; set; }
            public bool EnableAutoUndockAfterProcessComplete { get; set; }
            public bool EnableAutoUndockAfterUndocking { get; set; }
            public bool EnablePauseWhenVerifyIDFailed { get; set; }
           
            public bool ConfigDO32AsBuzzer { get; set; }
			public bool ShowWaferID { get; set; }

            public bool CycleEnable { get; set; }

            public int CycleCount { get; set; }
            public bool CycleEnableAutoUnload { get; set; }

            public bool CycleEnableAlign { get; set; }
            public bool CycleEnableLaserMarker1 { get; set; }
            public bool CycleEnableLaserMarker2 { get; set; }
            public bool TransferStopImmediately { get; set; }

            public bool OptionLoadportMonitor { get; set; }
            public bool OptionMainternaceMonitor { get; set; }
            public bool OptionSignalTowerMonitor { get; set; }

            public bool FaEnableSpooling { get; set; }
            public string FaConnectionMode { get; set; }
            public string FaLocalIpAddress { get; set; }
            public int FaLocalPortNumber { get; set; }
            public string FaRemoteIpAddress { get; set; }
            public int FaRemotePortNumber { get; set; }
            public double FaT3Timeout { get; set; }
            public double FaT5Timeout { get; set; }
            public double FaT6Timeout { get; set; }
            public double FaT7Timeout { get; set; }
            public double FaT8Timeout { get; set; }
            public double FaLinkTestInterval { get; set; }
            public string FaDefaultCommunicationState { get; set; }
            public string FaDefaultControlState { get; set; }
            public string FaDefaultControlSubState { get; set; }
            public int FaDeviceId { get; set; }

            public string WaferDisplayMode { get; set; }

            public bool EnableAligner { get; set; }
            public bool EnableOcrReader { get; set; }
        }
    }
}