/*
 * Copyright 2016 Christian Thomas.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.chrthms.hmatic4j.core.commands.impl.init;

import de.chrthms.hmatic4j.core.commands.impl.AbstractCommand;
import de.chrthms.hmatic4j.core.exceptions.HMExecutionException;
import de.chrthms.hmatic4j.core.exceptions.HMUnsupportedException;
import de.chrthms.hmatic4j.core.helpers.ConcatHelper;
import de.chrthms.hmatic4j.core.impl.AbstractConnectionImpl;

/**
 *
 * @author christian
 */
public abstract class AbstractInit extends AbstractCommand {

    private String logicRpcServerAddress = null;
    private String logicRpcServerPort = null;
    
    protected abstract String getIterfaceId() throws HMExecutionException;
    
    private String getLogicRpcServerUrl() throws HMExecutionException {
        if (logicRpcServerAddress == null) throw new HMExecutionException("An URL for the rpc server (logic side) must be set!");
        
        if (logicRpcServerPort != null) {
            return ConcatHelper.concatAddressPort(logicRpcServerAddress, logicRpcServerPort);
        } else {
            return logicRpcServerAddress;
        }
    }
    
    public AbstractInit logicRpcServerAddress(String logicRpcServerAddress) {
        this.logicRpcServerAddress = logicRpcServerAddress;
        return this;
    }
    
    public AbstractInit logicRpcServerPort(String logicRpcServerPort) {
        this.logicRpcServerPort = logicRpcServerPort;
        return this;
    }
    
    @Override
    public void execute(AbstractConnectionImpl connection) throws HMUnsupportedException, HMExecutionException {
        final String interfaceId = getIterfaceId();
        
        if (interfaceId != null) {
            connection.executeRpcCall(getMethodName(), getLogicRpcServerUrl(), interfaceId);            
        } else {
            connection.executeRpcCall(getMethodName(), getLogicRpcServerUrl());            
        }        
    }

    @Override
    public Object singleResult(AbstractConnectionImpl connection) throws HMUnsupportedException, HMExecutionException {
        throw new HMUnsupportedException();
    }

    @Override
    protected String getMethodName() {
        return "init";
    }

}

@@ -1146,24 +1146,69 @@
 ort = null;%0A
+    private String logicRpcServerPath = %22/%22;%0A
     %0A    pro
@@ -1483,32 +1483,96 @@
 et!%22);%0A        %0A
+        StringBuilder serverUrl = new StringBuilder();%0A        %0A
         if (logi
@@ -1609,23 +1609,33 @@
         
-return 
+serverUrl.append(
 ConcatHe
@@ -1699,16 +1699,17 @@
 verPort)
+)
 ;%0A      
@@ -1731,23 +1731,33 @@
         
-return 
+serverUrl.append(
 logicRpc
@@ -1761,32 +1761,33 @@
 RpcServerAddress
+)
 ;%0A        %7D%0A    
@@ -1778,24 +1778,221 @@
 ;%0A        %7D%0A
+        %0A        if (!logicRpcServerPath.startsWith(%22/%22)) %7B%0A            serverUrl.append(%22/%22);%0A        %7D%0A        %0A        serverUrl.append(logicRpcServerPath);%0A        return serverUrl.toString();%0A
     %7D%0A    %0A 
@@ -2314,24 +2314,182 @@
 %0A    %7D%0A    %0A
+    public AbstractInit logicRpcServerPath(String logicRpcServerPath) %7B%0A        this.logicRpcServerPath = logicRpcServerPath;%0A        return this;%0A    %7D%0A    %0A
     @Overrid

add "path" method for init commands