/*
   Copyright 2008-2013 CNR-ISTI, http://isti.cnr.it
   Institute of Information Science and Technologies
   of the Italian National Research Council


   See the NOTICE file distributed with this work for additional
   information regarding copyright ownership

   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 org.bubblecloud.zigbee.api.cluster.impl;


import org.bubblecloud.zigbee.api.ZigBeeDeviceException;
import org.bubblecloud.zigbee.api.cluster.general.DoorLock;
import org.bubblecloud.zigbee.api.cluster.impl.api.core.Attribute;
import org.bubblecloud.zigbee.api.cluster.impl.api.core.Reporter;
import org.bubblecloud.zigbee.v3.model.Status;
import org.bubblecloud.zigbee.api.cluster.impl.api.core.ZigBeeClusterException;
import org.bubblecloud.zigbee.api.cluster.impl.api.general.security.DoorLockResponse;
import org.bubblecloud.zigbee.api.cluster.impl.general.DoorLockCluster;
import org.bubblecloud.zigbee.network.ZigBeeEndpoint;

/**
 * 
 * @author <a href="mailto:giancarlo.riolo@isti.cnr.it">Giancarlo Riolo</a>
 * @version $LastChangedRevision:  $ ($LastChangedDate: $)
 *
 */

public class DoorLockImpl implements DoorLock {
	
	private DoorLockCluster doorLockCluster;


	public DoorLockImpl(ZigBeeEndpoint zbDevice){
		 doorLockCluster = new DoorLockCluster(zbDevice);
		
	}

	public int getId() {
		
		return doorLockCluster.getId();
	}

	public String getName() {
	
		return doorLockCluster.getName();
	}

    public Reporter[] getAttributeReporters() {
		return doorLockCluster.getAttributeReporters();
	}

	public Attribute[] getAttributes() {
	
		return doorLockCluster.getAvailableAttributes();
	}

	public Attribute getAttribute(int id) {
		Attribute[] attributes = doorLockCluster.getAvailableAttributes();
		for (int i = 0; i < attributes.length; i++) {
			if( attributes[i].getId() == id ) 
				return attributes[i];
		}
		return null;
	}

	public String getDescription() throws ZigBeeDeviceException {
		 try {
	            return (String) doorLockCluster.getAttributeDescription().getValue();
	        } catch (ZigBeeClusterException e) {
	            throw new ZigBeeDeviceException(e);
	        }
	}
	
    public void lock() throws ZigBeeDeviceException {
        try {
        	DoorLockResponse response = (DoorLockResponse)doorLockCluster.lock();
            if (response.getStatus() != Status.SUCCESS)
                throw new ZigBeeDeviceException(response.getStatus().toString());
        } catch (ZigBeeClusterException e) {
            throw new ZigBeeDeviceException(e);
        }
    }
    
    public void lock(String pinCode) throws ZigBeeDeviceException {
        try {
        	DoorLockResponse response = (DoorLockResponse)doorLockCluster.lock(pinCode);
            if (response.getStatus() != Status.SUCCESS)
                throw new ZigBeeDeviceException(response.getStatus().toString());
        } catch (ZigBeeClusterException e) {
            throw new ZigBeeDeviceException(e);
        }
    }

	public void unlock() throws ZigBeeDeviceException {
		try {
			DoorLockResponse response = (DoorLockResponse)doorLockCluster.unlock();
			if (response.getStatus() != Status.SUCCESS)
				throw new ZigBeeDeviceException(response.getStatus().toString());
		} catch (ZigBeeClusterException e) {
			throw new ZigBeeDeviceException(e);
		}
	}

	public void unlock(String pinCode) throws ZigBeeDeviceException {
		try {
			DoorLockResponse response = (DoorLockResponse)doorLockCluster.unlock(pinCode);
			if (response.getStatus() != Status.SUCCESS)
				throw new ZigBeeDeviceException(response.getStatus().toString());
		} catch (ZigBeeClusterException e) {
			throw new ZigBeeDeviceException(e);
		}
	}

	public int getLockState() throws ZigBeeDeviceException {
		 try {
	            return (int) (Integer) doorLockCluster.getAttributeLockState().getValue();
	        } catch (ZigBeeClusterException e) {
	            throw new ZigBeeDeviceException(e);
	        }
	}


}