package com.jpsoft.ehome.device;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EHomeDeviceManager {
    private static Map<String,Integer> deviceMap = new ConcurrentHashMap<>();

    public static void add(String name,int loginID){
        deviceMap.put(name,loginID);
    }

    public static void remove(String name){
        deviceMap.remove(name);
    }

    public static Integer get(String name){
        return deviceMap.get(name);
    }

    public static String findDeviceNoByLoginID(int loginID){
        String deviceNo = null;

        for (String name : deviceMap.keySet()) {
            if (deviceMap.get(name).intValue() == loginID){
                deviceNo = name;
                break;
            }
        }

        return deviceNo;
    }

    public static List<Map> onlineList(){
        List<Map> mapList = new ArrayList<>();

        for (String name : deviceMap.keySet()) {
            Map<String,Object> map = new HashMap<>();

            map.put("name",name);
            map.put("loginID", deviceMap.get(name));

            mapList.add(map);
        }

        return mapList;
    }
}
