using System;
using Fort23.UTool;

namespace Core.Utility
{
    public struct EncryptionFloat : EncryptionBasic<float>, IFormattable
    {
        public static Random random = new Random(123);
        private long _key;
        private long _value;
        private static float jingDu = 10000f;
        private event System.Action<float> _callBack;

        public float Value
        {
            get { return Decrypt(_value); }
            set
            {
                _value = Encryption(value);
                try
                {
                    _callBack?.Invoke(value);
                }
                catch (Exception e)
                {
                    LogTool.Exception(e);
                }
            }
        }

        public void AddCallBack(Action<float> callBack)
        {
            _callBack += callBack;
        }

        public void RemoveCallBack(Action<float> callBack)
        {
            _callBack -= callBack;
        }

        public float Decrypt(long v)
        {
            long lv = v ^ _key;
            float fv = lv / jingDu;
            return fv;
        }

        public long Encryption(float v)
        {
            long lv = (long)(v * jingDu);
            _key = (long)random.Next(1, int.MaxValue);
            return lv ^ _key;
        }

        public static explicit operator EncryptionFloat(float value)
        {
            EncryptionFloat encryptionLong = new EncryptionFloat();
            encryptionLong.Value = value;
            return encryptionLong;
        }

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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