/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (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.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.faces.demo.poll;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

//import org.icefaces.application.PortableRenderer;
//import org.icefaces.application.PushRenderer;

@ManagedBean(name = "systemInfo")
@ApplicationScoped
public class SystemInfo {
    private static final Logger LOGGER = Logger.getLogger(SystemInfo.class.getName());

    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    private String hostAddress;
    private String hostName;

//    private PortableRenderer portableRenderer;

//    private Timer timer;
//    private TimerTask timerTask =
//        new TimerTask() {
//            public void run() {
//                portableRenderer.render(Poll.POLL_GROUP_NAME);
//            }
//        };
    
    public SystemInfo() {
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException exception) {
            hostAddress =  "[UNKNOWN]";
        }
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException exception) {
            hostName = "[UNKNOWN]";
        }
//        portableRenderer = PushRenderer.getPortableRenderer();
    }

    public String getCurrentTime() {
        return DATE_FORMAT.format(System.currentTimeMillis());
    }

    public String getHostAddress() {
        return hostAddress;
    }

    public String getHostName() {
        return hostName;
    }

    public static SystemInfo getInstance() {
        return (SystemInfo)FacesUtils.getManagedBean("systemInfo");
    }

    public String getServerInfo() {
        return FacesUtils.getServletContext().getServerInfo();
    }

    @PostConstruct
    public void setUp() {
//        long _delay = -1;
//        while (_delay < 0) {
//            Calendar _calendar = Calendar.getInstance();
//            _calendar.set(Calendar.SECOND, 0);
//            _calendar.set(Calendar.MILLISECOND, 0);
//            _calendar.roll(Calendar.MINUTE, true);
//            _delay = _calendar.getTimeInMillis() - System.currentTimeMillis();
//        }
//        timer = new Timer();
//        timer.scheduleAtFixedRate(timerTask, _delay, 60000);
    }

    @PreDestroy
    public void tearDown() {
//        timerTask.cancel();
//        timer.cancel();
//        timer = null;
    }
}
