/*
 * 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.facelets.component.util;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;

@ViewScoped
@ManagedBean(name = "browser")
public class BrowserDetector implements Serializable {

    private Integer currentBrowser;

    public static BrowserDetector getInstance() {
        return (BrowserDetector) Util.getManagedBean("browser");
    }

    public static int IE = 0, IE7 = 1, IE8 = 3, SAFARI = 4, SAFARI_MOBILE = 5, OPERA = 6, OPERA_MOBILE = 7,
            FIREFOX = 8;

    public boolean isIe7() {
        return getBrowserType() == IE7;
    }

    public boolean isIe8() {
        return getBrowserType() == IE8;
    }

    public boolean isIe() {
        return getBrowserType() == IE;
    }

    public boolean isFirefox() {
        return getBrowserType() == FIREFOX;
    }

    public boolean isOpera() {
        return getBrowserType() == OPERA;
    }

    public boolean isAnyIe() {
        return getBrowserType() == IE || getBrowserType() == IE7
                || getBrowserType() == IE8;
    }

    public int getBrowserType() {
        if (currentBrowser == null) {
            detectBrowser();
        }
        return currentBrowser;
    }

    private void detectBrowser() {
        currentBrowser = FIREFOX;//default
        Object userAgentObj = FacesContext.getCurrentInstance().getExternalContext().getRequestHeaderMap().get("user-agent");
        if (userAgentObj != null) {
            String userAgent = ((String) userAgentObj).toLowerCase();
            if (userAgent.indexOf("opera") < 0 && userAgent.indexOf("msie") != -1) {

                if (userAgent.indexOf("msie 7") != -1) {
                    currentBrowser = IE7;
                } else if (userAgent.indexOf("msie 8.0") != -1) {
                    currentBrowser = IE8;
                } else {
                    currentBrowser = IE;
                }
            } else if (userAgent.indexOf("safari") != -1) {
                currentBrowser = SAFARI;
                if (userAgent.indexOf("mobile") != -1) {
                    currentBrowser = SAFARI_MOBILE;
                }
            } else if (userAgent.indexOf("opera") != -1) {
                currentBrowser = OPERA;
                if (userAgent.indexOf("240x320") != -1) {
                    currentBrowser = OPERA_MOBILE;
                }
            }
        }
    }

    public String getBestImgFormat() {
        return isIe() ? "gif" : "png";
    }

    public boolean isLessThanIe8() {
        return getBrowserType() == IE7 || getBrowserType() == IE;
    }
}
