package com.intuso.housemate.client.proxy.api.bridge.v1_0;

import com.intuso.housemate.client.api.bridge.v1_0.object.ObjectMapper;
import com.intuso.housemate.client.api.bridge.v1_0.object.TypeInstancesMapper;
import com.intuso.housemate.client.api.internal.object.Object;
import com.intuso.housemate.client.api.internal.object.Type;
import com.intuso.housemate.client.api.internal.object.Value;
import com.intuso.housemate.client.api.internal.object.ValueBase;
import com.intuso.housemate.client.proxy.api.internal.ChildUtil;
import com.intuso.housemate.client.proxy.api.internal.object.JMSUtil;
import com.intuso.utilities.collection.ManagedCollectionFactory;
import org.slf4j.Logger;

import javax.jms.Connection;
import javax.jms.JMSException;

/**
 * Created by tomc on 28/11/16.
 */
public abstract class ProxyValueBaseBridge<
        VERSION_DATA extends com.intuso.housemate.client.v1_0.api.object.Object.Data,
        INTERNAL_DATA extends Object.Data,
        LISTENER extends ValueBase.Listener<? super VALUE>,
        VALUE extends ProxyValueBaseBridge<VERSION_DATA, INTERNAL_DATA, LISTENER, VALUE>>
        extends ProxyObjectBridge<VERSION_DATA, INTERNAL_DATA, LISTENER>
        implements ValueBase<Type.Instances, ProxyTypeBridge, LISTENER, VALUE> {

    private final TypeInstancesMapper typeInstancesMapper;

    private Type.Instances value;

    private JMSUtil.Sender valueSender;
    private com.intuso.housemate.client.proxy.api.internal.object.JMSUtil.Receiver<com.intuso.housemate.client.v1_0.api.object.Type.Instances> valueReceiver;

    protected ProxyValueBaseBridge(Logger logger,
                                   Class<INTERNAL_DATA> versionDataClass,
                                   ObjectMapper<VERSION_DATA, INTERNAL_DATA> dataMapper,
                                   TypeInstancesMapper typeInstancesMapper,
                                   ManagedCollectionFactory managedCollectionFactory) {
        super(logger, versionDataClass, dataMapper, managedCollectionFactory);
        this.typeInstancesMapper = typeInstancesMapper;
    }

    @Override
    protected void initChildren(String versionName, String internalName, Connection connection) throws JMSException {
        super.initChildren(versionName, internalName, connection);
        valueSender = new JMSUtil.Sender(logger, connection, JMSUtil.Type.Topic, ChildUtil.name(internalName, Value.VALUE_ID));
        valueReceiver = new com.intuso.housemate.client.proxy.api.internal.object.JMSUtil.Receiver<>(logger, connection, JMSUtil.Type.Topic, com.intuso.housemate.client.proxy.api.internal.ChildUtil.name(versionName, Value.VALUE_ID), com.intuso.housemate.client.v1_0.api.object.Type.Instances.class,
                new com.intuso.housemate.client.proxy.api.internal.object.JMSUtil.Receiver.Listener<com.intuso.housemate.client.v1_0.api.object.Type.Instances>() {
            @Override
            public void onMessage(com.intuso.housemate.client.v1_0.api.object.Type.Instances instances, boolean wasPersisted) {
                value = typeInstancesMapper.map(instances);
                // todo call object listeners
            }
        });
    }

    @Override
    protected void uninitChildren() {
        super.uninitChildren();
        if(valueSender != null) {
            valueSender.close();
            valueSender = null;
        }
        if(valueReceiver != null) {
            valueReceiver.close();
            valueReceiver = null;
        }
    }

    @Override
    public ProxyTypeBridge getType() {
        return null; // todo get the type from somewhere
    }

    @Override
    public Type.Instances getValue() {
        return value;
    }
}

@@ -1360,48 +1360,8 @@
 e;%0A%0A
-    private JMSUtil.Sender valueSender;%0A
     
@@ -1434,61 +1434,57 @@
 til.
-Receiver%3Ccom.intuso.housemate.client.v1_0.api.object.
+Sender valueSender;%0A    private JMSUtil.Receiver%3C
 Type
@@ -2259,137 +2259,96 @@
 new 
-JMSUtil.Sender(logger, connection, JMSUtil.Type.Topic, ChildUtil.name(internalName, Value.VALUE_ID));%0A        valueReceiver = new
+com.intuso.housemate.client.proxy.api.internal.object.JMSUtil.Sender(logger, connection,
  com
@@ -2402,47 +2402,8 @@
 ect.
-JMSUtil.Receiver%3C%3E(logger, connection, 
 JMSU
@@ -2496,25 +2496,8 @@
 ame,
- Value.VALUE_ID),
  com
@@ -2541,174 +2541,227 @@
 ect.
-Type.Instances.class,%0A                new com.intuso.housemate.client.proxy.api.internal.object.JMSUtil.Receiver.Listener%3Ccom.intuso.housemate.client.v1_0.api.object.
+Value.VALUE_ID));%0A        valueReceiver = new JMSUtil.Receiver%3C%3E(logger, connection, JMSUtil.Type.Topic, ChildUtil.name(internalName, Value.VALUE_ID), Type.Instances.class,%0A                new JMSUtil.Receiver.Listener%3C
 Type
@@ -2836,52 +2836,8 @@
 age(
-com.intuso.housemate.client.v1_0.api.object.
 Type
@@ -2910,43 +2910,251 @@
 e = 
-typeInstancesMapper.map(instances);
+instances;%0A                try %7B%0A                    valueSender.send(typeInstancesMapper.map(instances), wasPersisted);%0A                %7D catch (JMSException e) %7B%0A                    logger.error(%22Failed to send new value%22);%0A                %7D
 %0A   

Use the correct internal/version setup when proxying values