using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Aitex.Core.RT.Log;
using Aitex.Sorter.Common;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Equipment;
using Aitex.Core.RT.Device;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot
{
    public interface ITransferMsg
    {
        IDevice Robot { set; }
        bool background { get; }
        bool evt { get; }
        string package(params object[] args);
        /// 
        /// return value, completed
        /// 
        /// 
        /// 
        bool unpackage(string type, string[] cmd);
    }
    public class TokenGenerator
    {
        private int _last = 0;
        List _pool = new List();
        SCConfigItem scToken = null;
        public TokenGenerator(string scName) 
        {
            scToken = SC.GetConfigItem(scName);
            if (scToken == null)
            _last = scToken.IntValue;
            Random r = new Random();
            _last = r.Next() % 20;
        }
 
        public int create()
        {
            int first = _last;
            int token = first;
            do
            {
                token = (token + 1) % 100;
                if (_pool.Contains(token))
                    continue;
                _pool.Add(token);
                _last = token;
                scToken.IntValue = _last;
                return _last;
            } while (token != first);
            throw (new ExcuteFailedException("Get token failed,pool is full"));
        }
        public void release(int token)
        {
            _pool.Remove(token);
        }
        public void release()
        {
            _last = 0;
            _pool.Clear();
        }
    }
    public interface IRobotHandlerFactory
    {
        IHandler Init();
        IHandler Home();
        IHandler Event();
        IHandler Grip(Hand hand);
        IHandler Release(Hand hand);
        IHandler QueryState();
        IHandler QueryPosition();
        IHandler Clear();
        IHandler Stop();
        IHandler SetSpeed(int speed);
        IHandler Pick(ModuleName chamber, int slot, Hand hand);
        IHandler PickExtend(ModuleName chamber, int slot, Hand hand);
        IHandler PickRetract(ModuleName chamber, int slot, Hand hand);
        IHandler Place(ModuleName chamber, int slot, Hand hand);
        IHandler PlaceExtend(ModuleName chamber, int slot, Hand hand);
        IHandler PlaceRetract(ModuleName chamber, int slot, Hand hand);
        IHandler Extend(ModuleName chamber, int slot, Hand hand);
        IHandler Retract(ModuleName chamber, int slot, Hand hand);
        IHandler Exchange(ModuleName chamber, int slot, Hand hand);
        IHandler Goto(ModuleName chamber, int slot, Motion next, Hand hand, int x,int y, int z);
    
        IHandler WaferMapping(ModuleName loadport);
        IHandler QueryWaferMap(ModuleName loadport);
        IHandler PickEx(ModuleName chamber, int slot, Hand hand, int x, int y, int z);
        IHandler PlaceEx(ModuleName chamber, int slot, Hand hand, int x, int y, int z);
        IHandler SetCommunication();
        IHandler SetLoad(Hand hand);
        IHandler CheckLoad(ModuleName chamber, int slot);
        IHandler RequestWaferPresent();
    }
}