package org.codingmatters.value.objects.js.generator.visitor;

import org.codingmatters.value.objects.js.error.ProcessingException;
import org.codingmatters.value.objects.js.generator.JsFileWriter;
import org.codingmatters.value.objects.js.generator.valueObject.JsClassGenerator;
import org.codingmatters.value.objects.js.parser.model.ParsedEnum;
import org.codingmatters.value.objects.js.parser.model.ParsedValueObject;
import org.codingmatters.value.objects.js.parser.model.ParsedYAMLSpec;
import org.codingmatters.value.objects.js.parser.model.ValueObjectProperty;
import org.codingmatters.value.objects.js.parser.model.types.*;
import org.codingmatters.value.objects.js.parser.processing.ParsedYamlProcessor;

import java.io.IOException;

public class JsValueListTypeReferenceProcessor implements ParsedYamlProcessor {
    private final JsFileWriter write;

    public JsValueListTypeReferenceProcessor( JsFileWriter write ) {
        this.write = write;
    }

    @Override
    public void process( ParsedYAMLSpec spec ) throws ProcessingException {

    }

    @Override
    public void process( ParsedValueObject valueObject ) throws ProcessingException {

    }

    @Override
    public void process( ValueObjectProperty property ) throws ProcessingException {

    }

    @Override
    public void process( ObjectTypeExternalValue externalValueObject ) throws ProcessingException {

    }

    @Override
    public void process( ObjectTypeInSpecValueObject inSpecValueObject ) throws ProcessingException {

    }

    @Override
    public void process( ObjectTypeNested nestedValueObject ) throws ProcessingException {

    }

    @Override
    public void process( ValueObjectTypeList list ) throws ProcessingException {
        list.type().process( this );
    }

    @Override
    public void process( ValueObjectTypePrimitiveType primitiveType ) throws ProcessingException {
        try{
            switch( primitiveType.type() ) {
                case BOOL:
                    write.string( "BooleanArray" );
                    break;
                case DATE:
                    write.string( "DateArray" );
                    break;
                case DATE_TIME:
                    write.string( "DateTimeArray" );
                    break;
                case DOUBLE:
                    write.string( "DoubleArray" );
                    break;
                case FLOAT:
                    write.string( "FloatArray" );
                    break;
                case INT:
                    write.string( "IntegerArray" );
                    break;
                case LONG:
                    write.string( "LongArray" );
                    break;
                case OBJECT:
                    write.string( "ObjectArray" );
                    break;
                case STRING:
                case BYTES:
                    write.string( "StringArray" );
                    break;
                case TIME:
                    write.string( "TimeArray" );
                    break;
                case TZ_DATE_TIME:
                    write.string( "TzDateTimeArray" );
                    break;
                default:
                    throw new ProcessingException( "Cannot handle this type: " + primitiveType.type() );
            }
        } catch( IOException e ) {
            throw new ProcessingException( e );
        }
    }

    @Override
    public void process( YamlEnumExternalEnum externalEnum ) throws ProcessingException {
    }

    @Override
    public void process( YamlEnumInSpecEnum inSpecEnum ) throws ProcessingException {
        try{
            write.string( inSpecEnum.name() + "List" );
        } catch( IOException e ) {
            throw new ProcessingException( e );
        }
    }

    @Override
    public void process( ValueObjectTypeExternalType externalType ) throws ProcessingException {

    }

    @Override
    public void process( ParsedEnum parsedEnum ) throws ProcessingException {
        try{
            write.string( parsedEnum.name() + "List" );
        } catch( IOException e ) {
            throw new ProcessingException( e );
        }
    }
}

@@ -195,90 +195,8 @@
 er;%0A
-import org.codingmatters.value.objects.js.generator.valueObject.JsClassGenerator;%0A
 impo
@@ -1308,245 +1308,871 @@
 n %7B%0A
-%0A    %7D%0A%0A    @Override%0A    public void process( ObjectTypeInSpecValueObject inSpecValueObject ) throws ProcessingException %7B%0A%0A    %7D%0A%0A    @Override%0A    public void process( ObjectTypeNested nestedValueObject ) throws ProcessingException %7B%0A
+        try%7B%0A            write.string( externalValueObject.objectReference().substring( externalValueObject.objectReference().lastIndexOf( %22.%22 ) + 1 ) + %22List%22 );%0A        %7D catch( IOException e ) %7B%0A            throw new ProcessingException( e );%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void process( ObjectTypeInSpecValueObject inSpecValueObject ) throws ProcessingException %7B%0A        try%7B%0A            write.string( inSpecValueObject.inSpecValueObjectName() + %22List%22 );%0A        %7D catch( IOException e ) %7B%0A            throw new ProcessingException( e );%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void process( ObjectTypeNested nestedValueObject ) throws ProcessingException %7B%0A        try%7B%0A            write.string( nestedValueObject.nestValueObject().name() + %22List%22 );%0A        %7D catch( IOException e ) %7B%0A            throw new ProcessingException( e );%0A        %7D
 %0A   
@@ -4032,24 +4032,262 @@
 Exception %7B%0A
+        try%7B%0A            write.string( externalEnum.enumReference().substring( externalEnum.enumReference().lastIndexOf( %22.%22 ) + 1 ) + %22List%22 );%0A        %7D catch( IOException e ) %7B%0A            throw new ProcessingException( e );%0A        %7D%0A
     %7D%0A%0A    @
@@ -4653,32 +4653,265 @@
 singException %7B%0A
+        try%7B%0A            write.string( externalType.typeReference().substring( externalType.typeReference().indexOf( %22.%22 ) + 1 ) + %22List%22 );%0A        %7D catch( IOException e ) %7B%0A            throw new ProcessingException( e );%0A        %7D
 %0A    %7D%0A%0A    @Ove

Fix list type reference