using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DeviceLib.Model.AGV
{
    [Table("tbl_robots")]
    public class Robot
    {
        /// <summary>
        /// 机器人编号
        /// </summary>
        [Key]
        public int id { set; get; }
        /// <summary>
        /// 机器人IP
        /// </summary>
        public string ip { set; get; }
        /// <summary>
        /// 机器人名称
        /// </summary>
        public string name { set; get; }
        /// <summary>
        /// 所在位置
        /// </summary>
        public string place { set; get; }
        /// <summary>
        /// 是否使用调度系统
        /// </summary>
        public bool use_fleet { get; set; } = false;
        /// <summary>
        /// 在调度系统中的id
        /// </summary>
        public int id_in_fleet { get; set; }
        /// <summary>
        /// 调度系统id
        /// </summary>
        public int fleet_id { get; set; }
        [NotMapped]
        public Fleet fleet { get; set; }
        /// <summary>
        /// 自动模式
        /// </summary>
        public bool auto { get; set; }
        /// <summary>
        /// 是否是调试状态,true时不执行新任务
        /// </summary>
        public bool is_debug { get; set; }
        /// <summary>
        /// 机器人类型,1是mir
        /// </summary>
        public int type { get; set; }
        /// <summary>
        /// 车间编号
        /// </summary>
        public int workshop_id { get; set; }
        [NotMapped]
        public Workshop workshop { get; set; }
        /// <summary>
        /// 任务组id
        /// </summary>
        public int group_id { get; set; }
        [NotMapped]
        public MissionGroup mission_group { get; set; }
        /// <summary>
        /// IO 模块的id,多个使用#号分隔
        /// </summary>
        public string io_module_ids { get; set; }
        [NotMapped]
        public List<IOModule> io_modules { get; set; }
        [NotMapped]
        /// <summary>
        /// AGV状态
        /// </summary>
        public Status status { set; get; }
        /// <summary>
        /// AGV任务运行状态
        /// </summary>
        [NotMapped]
        public MissionState mission_state { set; get; }
        /// <summary>
        /// 机器人自定义状态
        /// </summary>
        [NotMapped]
        public RobotState state { set; get; } = RobotState.空闲;
        [NotMapped]
        public Job job { get; set; }
        /// <summary>
        /// 负载信息
        /// </summary>
        [NotMapped]
        public List<LoadInfo> load_infos { get; set; }
        [NotMapped]
        public RobotOperation operation;
        #region 方法
        public Robot()
        {
            threadGetStatus = new System.Threading.Thread(getStatus);
            threadGetStatus.IsBackground = true;
            threadGetStatus.Start();
        }
        System.Threading.Thread threadGetStatus;
        private void getStatus()
        {
            while (true)
            {
                try
                {
                    if (operation?.Get_Status(out Status status) ?? false)
                    {
                        this.status = status;
                        logRobotOnline(this.status, true);
                    }
                    else
                    {
                        if(this.status==null)
                        {
                            this.status = new Status();
                        }
                        else
                        {
                            logRobotOnline(this.status,false);
                        }
                    }
                    operation?.Get_IO_Status();
                    System.Threading.Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    LogUtil.Error($"【{ip}】【{name}】getStatus 出错", ex);
                }
            }

        }
        private void logRobotOnline(Status status, bool online)
        {
            if (!status.is_online.Equals(online))
            {
                if (online)
                {
                    LogUtil.Info($"【{name}】在线");
                }
                else
                {
                    LogUtil.Info($"【{name}】离线");
                }
                status.is_online = online;
            }
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            Robot agv = obj as Robot;
            if (agv == null)
                return false;
            else
            {
                if (this.id.Equals(agv.id))
                    return true;
            }
            return false;
        }
        #endregion
    }
}