using System;
using Fort23.UTool;

namespace Core.Utility
{
    public struct EncryptionInt: EncryptionBasic<int>, IFormattable
    {
        public static Random random = new Random(123);
        private long _key;
        private long _value;
        private event System.Action<int> _callBack;
        public int Value
        {
            get { return Decrypt(_value); }
            set
            {
                _value = Encryption(value); 
                try
                {
                    _callBack?.Invoke(value);
                }
                catch (Exception e)
                {
                    LogTool.Exception(e);
                }
            }
        }

        public int Decrypt(long v)
        {
            return (int)(v ^ _key);
        }

        public long Encryption(int v)
        {
            _key = random.Next(1, int.MaxValue);
            return v ^ _key;
        }
        public void AddCallBack(Action<int> callBack)
        {
            _callBack += callBack;
        }

        public void RemoveCallBack(Action<int> callBack)
        {
            _callBack -= callBack;
        }
        public static explicit operator EncryptionInt(long value)
        {
            EncryptionInt EncryptionInt = new EncryptionInt();
            EncryptionInt.Value = (int)value;
            return EncryptionInt;
        }

        public static explicit operator EncryptionInt(int value)
        {
            EncryptionInt EncryptionInt = new EncryptionInt();
            EncryptionInt.Value = value;
            return EncryptionInt;
        }


        public static bool operator ==(EncryptionInt x, EncryptionInt y)
        {
            return x.Value == y.Value;
        }

        public static bool operator !=(EncryptionInt x, EncryptionInt y)
        {
            return x.Value != y.Value;
        }

        public static bool operator >(EncryptionInt x, float y)
        {
            return x.Value > y;
        }

        public static bool operator <(EncryptionInt x, float y)
        {
            return x.Value < y;
        }

        public static bool operator >(float x, EncryptionInt y)
        {
            return x > y.Value;
        }

        public static bool operator <(float x, EncryptionInt y)
        {
            return x < y.Value;
        }

        public static bool operator >(EncryptionInt x, EncryptionInt y)
        {
            return x.Value > y.Value;
        }

        public static bool operator <(EncryptionInt x, EncryptionInt y)
        {
            return x.Value < y.Value;
        }

        public static bool operator >=(int x, EncryptionInt y)
        {
            return x >= y.Value;
        }

        public static bool operator <=(int x, EncryptionInt y)
        {
            return x <= y.Value;
        }

        public static bool operator >=(float x, EncryptionInt y)
        {
            return x >= y.Value;
        }

        public static bool operator <=(float x, EncryptionInt y)
        {
            return x <= y.Value;
        }

        public static bool operator >=(EncryptionInt x, int y)
        {
            return x.Value >= y;
        }

        public static bool operator <=(EncryptionInt x, int y)
        {
            return x.Value <= y;
        }

        public static bool operator >=(EncryptionInt x, float y)
        {
            return x.Value >= y;
        }

        public static bool operator <=(EncryptionInt x, float y)
        {
            return x.Value <= y;
        }

        public static bool operator >=(EncryptionInt x, EncryptionInt y)
        {
            return x.Value >= y.Value;
        }

        public static bool operator <=(EncryptionInt x, EncryptionInt y)
        {
            return x.Value <= y.Value;
        }

        public static EncryptionInt operator /(EncryptionInt x, int y)
        {
            return (EncryptionInt) (x.Value / y);
        }

        public static EncryptionInt operator /(int x, EncryptionInt y)
        {
            return (EncryptionInt) (x / y.Value);
        }


#if !COMBAT_SERVER
#endif
        public static EncryptionInt operator /(EncryptionInt x, EncryptionInt y)
        {
            return (EncryptionInt) (x.Value / y.Value);
        }

        public static EncryptionInt operator *(int x, EncryptionInt y)
        {
            return (EncryptionInt) (x * y.Value);
        }

        public static EncryptionInt operator *(EncryptionInt x, int y)
        {
            return (EncryptionInt) (x.Value * y);
        }

        public static EncryptionInt operator *(long x, EncryptionInt y)
        {
            return (EncryptionInt) (x * y.Value);
        }

        public static EncryptionInt operator *(EncryptionInt x, long y)
        {
            return (EncryptionInt) (x.Value * y);
        }

        public static EncryptionInt operator *(EncryptionInt x, EncryptionInt y)
        {
            return (EncryptionInt) (x.Value * y.Value);
        }


        public static EncryptionInt operator +(int x, EncryptionInt y)
        {
            return (EncryptionInt) (x + y.Value);
        }

        public static EncryptionInt operator +(EncryptionInt x, int y)
        {
            return (EncryptionInt) (x.Value + y);
        }

        public static EncryptionInt operator +(long x, EncryptionInt y)
        {
            return (EncryptionInt) (x + y.Value);
        }
        public static string operator +(EncryptionInt x, string y)
        {
            return  (x.Value + y);
        }

        public static string operator +(string x, EncryptionInt y)
        {
            return  (x + y.Value);
        }
        public static EncryptionInt operator +(EncryptionInt x, long y)
        {
            return (EncryptionInt) (x.Value + y);
        }

        public static EncryptionInt operator +(EncryptionInt x, EncryptionInt y)
        {
            return (EncryptionInt) (x.Value + y.Value);
        }

        public static EncryptionInt operator -(int x, EncryptionInt y)
        {
            return (EncryptionInt) (x - y.Value);
        }

        public static EncryptionInt operator -(EncryptionInt x, int y)
        {
            return (EncryptionInt) (x.Value - y);
        }

        public static EncryptionInt operator -(long x, EncryptionInt y)
        {
            return (EncryptionInt) (x - y.Value);
        }

        public static EncryptionInt operator -(EncryptionInt x, long y)
        {
            return (EncryptionInt) (x.Value - y);
        }

        public static EncryptionInt operator -(EncryptionInt x, EncryptionInt y)
        {
            return (EncryptionInt) (x.Value - y.Value);
        }


        // public static long operator -(int x, EncryptionInt y)
        // {
        //     return x - y.Value;
        // }
        //
        // public static long operator -(EncryptionInt x, int y)
        // {
        //     return x.Value - y;
        // }
        // public static long operator -(long x, EncryptionInt y)
        // {
        //     return x - y.Value;
        // }
        //
        // public static long operator -(EncryptionInt x, long y)
        // {
        //     return x.Value - y;
        // }
        public string ToString(string format, IFormatProvider formatProvider)
        {
            return Value.ToString();
        }
    }
}