/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package web.component.impl.awselb.model;

import com.amazonaws.services.elasticloadbalancing.model.Instance;
import com.amazonaws.services.elasticloadbalancing.model.InstanceState;
import web.component.api.model.BackendInstance;
import web.component.api.model.BackendInstanceState;
import web.component.api.model.LoadBalancer;

/**
 *
 * @author Hiroshi
 */
public class BackendInstanceImpl extends Instance implements BackendInstance{

    private LoadBalancer lb;
    private final String id;
    
    private BackendInstanceImpl(String id){
        this.id = id;
    }
    
    private BackendInstanceImpl(LoadBalancer lb, String id){
        this.lb = lb;
        this.id = id;
    }
    
    public static BackendInstance create(String id){
        return new BackendInstanceImpl(id);
    }

    public static BackendInstance create(LoadBalancer lb, String id){
        return new BackendInstanceImpl(lb,id);
    }

    @Override
    public LoadBalancer getLoadBalancer() {
        return lb;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void register(LoadBalancer newLb) {
        if(lb == null || !lb.equals(newLb)){
            newLb.registerInstance(this);
            lb = newLb;
        }
    }

    @Override
    public LoadBalancer deregister() {
        
        if(lb == null)
            return null;
        
        lb.deregisterInstance(this);
        LoadBalancer deregisteredFrom = lb;
        lb = null;
        return deregisteredFrom;
    }

    @Override
    public boolean equals(Object toCompare){
        if(toCompare instanceof BackendInstanceImpl)
            return this.getId().equals(((BackendInstanceImpl)toCompare).getId());
        return false;
    }

    @Override
    public int hashCode() {
        //this is wrong, but I don't know how to implement this method.
        return 31 * id.hashCode();
    }

    @Override
    public BackendInstanceState getBackendInstanceState(){
        return lb == null ? BackendInstanceImpl.State.create(new InstanceState()) : lb.getInstanceState(this);
    }
    
    public static class State implements BackendInstanceState{

        private final InstanceState elbInstanceState;
        
        private State(InstanceState elbInstanceState){
         this.elbInstanceState = elbInstanceState;
        }
        
        public static State create(InstanceState elbInstanceState){
            return new State(elbInstanceState);
        }
        
        @Override
        public String getDescription() {
            return elbInstanceState.getDescription();
        }

        @Override
        public String getId() {
            return elbInstanceState.getInstanceId();
        }

        @Override
        public String getReasonCode() {
            return elbInstanceState.getReasonCode();
        }

        @Override
        public String getState() {
            return elbInstanceState.getState();
        }
        
    }
}

@@ -628,16 +628,27 @@
 private 
+final List%3C
 LoadBala
@@ -651,19 +651,41 @@
 Balancer
+%3E
  lb
+s = new ArrayList%3C%3E()
 ;%0A    pr
@@ -862,20 +862,163 @@
     
-this.lb = lb
+%0A        if(!(lb instanceof LoadBalancerImpl))%0A            throw new IllegalArgumentException(%22Invalid load balancer specified.%22);%0A        this.lbs.add(lb)
 ;%0A  
@@ -1351,16 +1351,24 @@
 eturn lb
+s.get(0)
 ;%0A    %7D%0A
@@ -1360,32 +1360,127 @@
 .get(0);%0A    %7D%0A%0A
+    @Override%0A    public List%3CLoadBalancer%3E getLoadBalancers()%7B%0A        return lbs;%0A    %7D%0A    %0A
     @Override%0A  
@@ -1604,34 +1604,178 @@
     
-if(lb == null %7C%7C !lb.equal
+%0A        if(!(newLb instanceof LoadBalancerImpl))%0A            throw new IllegalArgumentException(%22Invalid load balancer specified.%22);%0A            %0A        if(!lbs.contain
 s(ne
@@ -1841,16 +1841,20 @@
   lb
- = 
+s.add(
 newLb
+)
 ;%0A  
@@ -1897,234 +1897,317 @@
 lic 
-LoadBalancer deregister() %7B%0A        %0A        if(lb == null)%0A            return null;%0A        %0A        lb.deregisterInstance(this);%0A        LoadBalancer deregisteredFrom = lb;%0A        lb = null;%0A        return deregisteredFrom;
+void deregisterFrom(LoadBalancer lb) %7B%0A        %0A        if(lb == null %7C%7C !(lb instanceof LoadBalancerImpl))%0A            throw new IllegalArgumentException(%22Invalid load balancer specified.%22);%0A        %0A        if(lbs.contains(lb))%7B%0A            lb.deregisterInstance(this);%0A            lbs.remove(lb);%0A        %7D
 %0A   

Update BackendInstanceImpl.java