/*
 * 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.aws.model;

import com.amazonaws.services.ec2.model.AvailabilityZone;
import java.util.HashMap;
import java.util.Map;
import web.component.api.model.Zone;

/**
 *
 * @author Hiroshi
 */
public class ZoneImpl extends AWSModelBase implements Zone{

    private static final Map<String,Zone> existZones = new HashMap<>();
    
    private final AvailabilityZone ec2Zone = new AvailabilityZone();
    
    private ZoneImpl(Builder builder){
        
        ec2Zone.setZoneName(builder.name);
    }
    
    public AvailabilityZone asEc2Zone(){
        return ec2Zone;
    }
    
    @Override
    public String getName(){
        return ec2Zone.getZoneName();
    }
    
    @Override
    public String getRegionName(){
        
        String regionName = ec2Zone.getRegionName();
        
        if(regionName == null || regionName.isEmpty()){

            regionName = "";

            try{
                regionName = ec2().getEc2AvailabilityZone(getName()).getRegionName();
                ec2Zone.setRegionName(regionName);
            }catch(RuntimeException e){
                //do nothing.
            }
        }
        
        return regionName;    
    }
    
    @Override
    public String getState(){
        
        String state = "Unknown state.";
        
        try{
            state = ec2().getEc2AvailabilityZone(getName()).getState();
        }catch(RuntimeException e){
            //do nothing.
        }
        
        return state;    
    }
    
    @Override
    public boolean equals(Object toBeCompared){
        if(toBeCompared instanceof ZoneImpl){
            ZoneImpl asZoneImpl = (ZoneImpl)toBeCompared;
            return getName().equals(asZoneImpl.getName()) && getRegionName().equals(asZoneImpl.getRegionName());
        }
        return false;
    }
    
    @Override
    public int hashCode(){
        //this is wrong, but don't know how to implement this method properly.
        return 31 * ( getName().hashCode() + getRegionName().hashCode() );
    }

    @Override
    public String toString(){
        return "{ZoneName: " + getName() + ", RegionName: " + getRegionName() + "}";
    }
    
    public static class Builder {
        
        private String name;
        
        public Builder name(String name){
            this.name = name;
            return this;
        }
        
        public Zone build(){
            if(existZones.get(name) == null)
                existZones.put(name, new ZoneImpl(this));
            return existZones.get(name);
        }
    }
}

@@ -276,16 +276,81 @@
 tyZone;%0A
+import com.amazonaws.services.ec2.model.AvailabilityZoneMessage;%0A
 import j
@@ -729,94 +729,183 @@
     
-ec2Zone.setZoneName(builder.name);%0A    %7D
+AvailabilityZone source = ec2().getEc2AvailabilityZone(builder.name);%0A        ec2Zone.setMessages(source.getMessages());
 %0A    
-%0A
     
-public AvailabilityZone asEc2Zone()%7B
+ec2Zone.setRegionName(source.getRegionName());
 %0A   
@@ -913,73 +913,98 @@
     
-return ec2Zone;%0A    %7D%0A    %0A    @Override%0A    public String getNam
+ec2Zone.setZoneName(source.getZoneName());%0A    %7D%0A    %0A    public AvailabilityZone asEc2Zon
 e()%7B
@@ -1026,30 +1026,16 @@
  ec2Zone
-.getZoneName()
 ;%0A    %7D%0A
@@ -1068,28 +1068,32 @@
 lic 
+List%3C
 String
+%3E
  get
-RegionName
+Messages
 ()%7B%0A
@@ -1104,34 +1104,46 @@
     
-%0A        String regionName
+List%3CAvailabilityZoneMessage%3E messages
  = e
@@ -1148,34 +1148,32 @@
  ec2Zone.get
-RegionName
+Messages
 ();%0A        
@@ -1176,328 +1176,286 @@
     
-%0A        if(regionName
+List%3CString%3E messageStrings
  =
-=
  n
-ull %7C%7C regionName.isEmpty())%7B%0A%0A            regionName = %22%22;%0A%0A            try%7B%0A                regionName = ec2().getEc2AvailabilityZone(getName()).getRegionName();%0A                ec2Zone.setRegionName(regionName);%0A            %7Dcatch(RuntimeException e)%7B
+ew ArrayList%3C%3E();%0A        for(AvailabilityZoneMessage message : messages)%0A            messageStrings.add(message.getMessage());%0A        return messageStrings;
 %0A    
+%7D%0A%0A
     
-        //do nothing.%0A        
+@Override%0A    public String getName()%7B%0A        return ec2Zone.getZoneName();%0A
     
@@ -1456,38 +1456,73 @@
 ;%0A    %7D%0A    
+%0A
     
-%7D%0A        
+@Override%0A    public String getRegionName()%7B
 %0A        ret
@@ -1525,17 +1525,28 @@
  return 
-r
+ec2Zone.getR
 egionNam
@@ -1546,16 +1546,18 @@
 gionName
+()
 ;    %0A  

Update ZoneImpl.java