using System;
using UnityEngine;

namespace RebootKit.Engine.Foundation {
    [Serializable]
    public struct SerializableGuid : IEquatable<SerializableGuid> {
        public static SerializableGuid Zero = new SerializableGuid(0, 0, 0, 0);

        [SerializeField, HideInInspector] public uint A;
        [SerializeField, HideInInspector] public uint B;
        [SerializeField, HideInInspector] public uint C;
        [SerializeField, HideInInspector] public uint D;

        public SerializableGuid(uint a, uint b, uint c, uint d) {
            A = a;
            B = b;
            C = c;
            D = d;
        }

        public SerializableGuid(Guid guid) {
            byte[] bytes = guid.ToByteArray();
            A = BitConverter.ToUInt32(bytes, 0);
            B = BitConverter.ToUInt32(bytes, 4);
            C = BitConverter.ToUInt32(bytes, 8);
            D = BitConverter.ToUInt32(bytes, 12);
        }

        public bool Equals(SerializableGuid other) {
            return A == other.A && B == other.B && C == other.C && D == other.D;
        }

        public override bool Equals(object obj) {
            return obj is SerializableGuid other && Equals(other);
        }

        public override int GetHashCode() {
            return HashCode.Combine(A, B, C, D);
        }

        public static SerializableGuid New() {
            return new SerializableGuid(Guid.NewGuid());
        }

        public static bool operator ==(SerializableGuid lhs, SerializableGuid rhs) {
            return lhs.Equals(rhs);
        }

        public static bool operator !=(SerializableGuid lhs, SerializableGuid rhs) {
            return !lhs.Equals(rhs);
        }
    }
}