using PSO2SERVER.Database;
using PSO2SERVER.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSO2SERVER.Models
{
    public class NetInterface
    {
        /// <summary>
        /// Interface status.
        /// </summary>
        public uint State { get; set; }

        /// <summary>
        /// Interface MAC address.
        /// </summary>
        public string Mac { get; set; } = new string('\0', 0x18); // 以字符串形式存储

        public void ReadFromStream(PacketReader reader)
        {
            State = reader.ReadUInt32();
            Mac = Encoding.ASCII.GetString(reader.ReadBytes(0x18)).TrimEnd('\0');
        }

        public override string ToString()
        {
            return $"状态: {State}, MAC: {Mac}";
        }

        public void UpdateNetInterface(int id, int accountid, NetInterface updatedInterface)
        {
            using (var db = new ServerEf())
            {
                var existingInterface = db.AccountsNetInterFaces
                    .FirstOrDefault(x => x.AccountId == accountid && x.id == id);

                if (existingInterface != null)
                {
                    // 更新其他字段
                    existingInterface.State = (int)updatedInterface.State;
                    existingInterface.Mac = updatedInterface.Mac;

                    // 保存更改
                    db.SaveChanges();
                }
            }
        }
    }

}
