/*
 *     Copyright (C) 2015  higherfrequencytrading.com
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.openhft.chronicle.wire;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.ref.BinaryLongArrayReference;
import net.openhft.chronicle.bytes.ref.BinaryLongReference;
import net.openhft.chronicle.bytes.ref.TextLongArrayReference;
import net.openhft.chronicle.bytes.ref.TextLongReference;
import net.openhft.chronicle.core.values.LongArrayValues;
import net.openhft.chronicle.core.values.LongValue;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * A selection of prebuilt wire types.
 */
public enum WireType implements Function<Bytes, Wire> {
    TEXT {
        @NotNull
        @Override
        public Wire apply(Bytes bytes) {
            return new TextWire(bytes);
        }

        @Override
        public Supplier<LongValue> newLongReference() {
            return TextLongReference::new;
        }

        @Override
        public Supplier<LongArrayValues> newLongArrayReference() {
            return TextLongArrayReference::new;
        }

    }, BINARY {
        @NotNull
        @Override
        public Wire apply(Bytes bytes) {
            return new BinaryWire(bytes);
        }

        @Override
        public String asString(WriteMarshallable marshallable) {
            return asHexString(marshallable);
        }

        @Override
        public <T> T fromString(CharSequence cs) {
            return fromHexString(cs);
        }
    }, FIELDLESS_BINARY {
        @NotNull
        @Override
        public Wire apply(Bytes bytes) {
            return new BinaryWire(bytes, false, false, true, Integer.MAX_VALUE, "binary");
        }

        @Override
        public String asString(WriteMarshallable marshallable) {
            return asHexString(marshallable);
        }

        @Override
        public <T> T fromString(CharSequence cs) {
            return fromHexString(cs);
        }
    }, COMPRESSED_BINARY {
        @NotNull
        @Override
        public Wire apply(Bytes bytes) {
            return new BinaryWire(bytes, false, false, false, COMPRESSED_SIZE, "lzw");
        }

        @Override
        public String asString(WriteMarshallable marshallable) {
            return asHexString(marshallable);
        }

        @Override
        public <T> T fromString(CharSequence cs) {
            return fromHexString(cs);
        }
    }, JSON {
        @NotNull
        @Override
        public Wire apply(Bytes bytes) {
            return new JSONWire(bytes);
        }
    }, RAW {
        @NotNull
        @Override
        public Wire apply(Bytes bytes) {
            return new RawWire(bytes);
        }

        @Override
        public String asString(WriteMarshallable marshallable) {
            return asHexString(marshallable);
        }

        @Override
        public <T> T fromString(CharSequence cs) {
            return fromHexString(cs);
        }
    }, READ_ANY {
        @Override
        public Wire apply(@NotNull Bytes bytes) {
            int code = bytes.readByte(0);
            if (code >= ' ' && code < 127)
                return TEXT.apply(bytes);
            if (BinaryWireCode.isFieldCode(code))
                return FIELDLESS_BINARY.apply(bytes);
            return BINARY.apply(bytes);
        }
    };

    static final ThreadLocal<Bytes> bytesTL = ThreadLocal.withInitial(Bytes::allocateElasticDirect);
    private static final int COMPRESSED_SIZE = Integer.getInteger("WireType.compressedSize", 128);

    public Supplier<LongValue> newLongReference() {
        return BinaryLongReference::new;
    }

    public Supplier<LongArrayValues> newLongArrayReference() {
        return BinaryLongArrayReference::new;
    }

    public String asString(WriteMarshallable marshallable) {
        Bytes bytes = bytesTL.get();
        Wire wire = apply(bytes);
        wire.getValueOut().typedMarshallable(marshallable);
        return bytes.toString();
    }

    public <T> T fromString(CharSequence cs) {
        Bytes bytes = bytesTL.get();
        bytes.appendUtf8(cs);
        Wire wire = apply(bytes);
        return wire.getValueIn().typedMarshallable();
    }

    String asHexString(WriteMarshallable marshallable) {
        Bytes bytes = bytesTL.get();
        Wire wire = apply(bytes);
        wire.getValueOut().typedMarshallable(marshallable);
        return bytes.toHexString();
    }

    <T> T fromHexString(CharSequence s) {
        Wire wire = apply(Bytes.fromHexString(s.toString()));
        return wire.getValueIn().typedMarshallable();
    }
}
@@ -4216,16 +4216,135 @@
  128);%0A%0A
+    static Bytes getBytes() %7B%0A        Bytes bytes = bytesTL.get();%0A        bytes.clear();%0A        return bytes;%0A    %7D%0A%0A
     publ
@@ -4379,32 +4379,32 @@
 ngReference() %7B%0A
-
         return B
@@ -4630,35 +4630,32 @@
 tes bytes = 
-bytesTL.get
+getBytes
 ();%0A        
@@ -4845,35 +4845,32 @@
 tes bytes = 
-bytesTL.get
+getBytes
 ();%0A        
@@ -5061,35 +5061,32 @@
 tes bytes = 
-bytesTL.get
+getBytes
 ();%0A        

Add a simple asString/fromString for different wrie types.