package skadistats.clarity.decoder.s2;

import skadistats.clarity.decoder.unpacker.*;
import skadistats.clarity.decoder.unpacker.factory.s2.FloatUnpackerFactory;
import skadistats.clarity.decoder.unpacker.factory.s2.QAngleUnpackerFactory;
import skadistats.clarity.decoder.unpacker.factory.s2.UnpackerFactory;
import skadistats.clarity.decoder.unpacker.factory.s2.VectorUnpackerFactory;
import skadistats.clarity.model.s2.field.FieldProperties;

import java.util.HashMap;
import java.util.Map;

public class S2UnpackerFactory {

    private static final Map<String, UnpackerFactory> FACTORIES = new HashMap<>();
    static {
        // Floats
        FACTORIES.put("float32", new FloatUnpackerFactory());
        FACTORIES.put("CNetworkedQuantizedFloat", new FloatUnpackerFactory());
        FACTORIES.put("QAngle", new QAngleUnpackerFactory());

        // Specials
        FACTORIES.put("Vector", new VectorUnpackerFactory());
    }

    private static final Map<String, Unpacker> UNPACKERS = new HashMap<>();
    static {
        // Booleans
        UNPACKERS.put("bool", new BoolUnpacker());

        // Unsigned ints
        UNPACKERS.put("uint8", new IntVarUnsignedUnpacker());
        UNPACKERS.put("uint16", new IntVarUnsignedUnpacker());
        UNPACKERS.put("uint32", new IntVarUnsignedUnpacker());
        UNPACKERS.put("uint64", new LongVarUnsignedUnpacker());

        // Signed ints
        UNPACKERS.put("int8", new IntVarSignedUnpacker());
        UNPACKERS.put("int16", new IntVarSignedUnpacker());
        UNPACKERS.put("int32", new IntVarSignedUnpacker());
        UNPACKERS.put("int64", new LongVarSignedUnpacker());

        // Strings
        UNPACKERS.put("CUtlSymbolLarge", new StringZeroTerminatedUnpacker());
        UNPACKERS.put("char", new StringZeroTerminatedUnpacker());
        UNPACKERS.put("CUtlStringToken", new IntVarUnsignedUnpacker());

        // Enums
        UNPACKERS.put("gender_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("DamageOptions_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("RenderMode_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("RenderFx_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("attributeprovidertypes_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("CourierState_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("MoveCollide_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("MoveType_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("SolidType_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("ShopItemViewMode_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("SurroundingBoundsType_t", new IntVarUnsignedUnpacker());
        UNPACKERS.put("DOTA_SHOP_TYPE", new IntVarUnsignedUnpacker());
        UNPACKERS.put("DOTA_HeroPickState", new IntVarUnsignedUnpacker());

        // Handles
        UNPACKERS.put("CHandle", new IntVarUnsignedUnpacker());
        UNPACKERS.put("CEntityHandle", new IntVarUnsignedUnpacker());
        UNPACKERS.put("CGameSceneNodeHandle", new IntVarUnsignedUnpacker());
        UNPACKERS.put("CStrongHandle", new LongVarUnsignedUnpacker());

        // Colors
        UNPACKERS.put("Color", new IntVarUnsignedUnpacker());
        UNPACKERS.put("color32", new IntVarUnsignedUnpacker());

        // Specials
        UNPACKERS.put("HSequence", new IntMinusOneUnpacker());
    }
    

    public static Unpacker createUnpacker(FieldProperties fieldProperties, String type) {
        UnpackerFactory unpackerFactory = FACTORIES.get(type);
        if (unpackerFactory != null) {
            return unpackerFactory.createUnpacker(fieldProperties);
        }
        Unpacker unpacker = UNPACKERS.get(type);
//        if (unpacker == null) {
//            throw new RuntimeException("don't know how to create unpacker for " + type);
//        }
        return unpacker;
    }
}

@@ -33,16 +33,73 @@
 er.s2;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import s
@@ -136,16 +136,16 @@
 cker.*;%0A
-
 import s
@@ -579,16 +579,189 @@
 tory %7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(S2UnpackerFactory.class);%0A%0A    private static final Unpacker DEFAULT_UNPACKER = new IntVarUnsignedUnpacker();%0A%0A
     priv
@@ -2103,957 +2103,8 @@
 );%0A%0A
-        // Enums%0A        UNPACKERS.put(%22gender_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22DamageOptions_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22RenderMode_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22RenderFx_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22attributeprovidertypes_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22CourierState_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22MoveCollide_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22MoveType_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22SolidType_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22ShopItemViewMode_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22SurroundingBoundsType_t%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22DOTA_SHOP_TYPE%22, new IntVarUnsignedUnpacker());%0A        UNPACKERS.put(%22DOTA_HeroPickState%22, new IntVarUnsignedUnpacker());%0A%0A
     
@@ -2956,26 +2956,24 @@
 .get(type);%0A
-//
         if (
@@ -2992,18 +2992,16 @@
 null) %7B%0A
-//
         
@@ -3008,34 +3008,17 @@
     
-throw new RuntimeException
+log.debug
 (%22do
@@ -3057,21 +3057,76 @@
 for 
-%22 + type);%0A//
+%7B%7D, assuming int.%22, type);%0A            unpacker = DEFAULT_UNPACKER;%0A
     

UIntDecoder is default