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

import com.icesoft.faces.context.JarResource;
import com.icesoft.faces.context.ResourceRegistry;
import com.icesoft.faces.context.effects.JavascriptContext;

import javax.faces.context.FacesContext;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

public class StatusBarController implements Serializable {

    private static final String PATH_BASE = "com/icesoft/faces/facelets/component/statusbar/";
    private static final String BG_IMAGE = "status-bar-bg.png";
    private static final String LEFT_IMAGE = "status-bar-left.png";
    private static final String RIGHT_IMAGE = "status-bar-right.png";

    private int speed = 5;
    private String bgImagePath;
    private String leftImagePath;
    private String rightImagePath;

    public StatusBarController() {
        registerJavascriptLibrary();
        registerImages();
        startScrolling();
    }

    public void startScrolling() {
        JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "initMarquee();");
    }

    public void stopScrolling() {
        JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "stopMarquee();");
    }

    public void setSpeed() {

    }

    public void getSpeed() {

    }

    public String getBgImagePath() {
        return bgImagePath;
    }

    public String getLeftImagePath() {
        return leftImagePath;
    }

    public String getRightImagePath() {
        return rightImagePath;
    }

    private void registerJavascriptLibrary() {
        //JarResource jsResource = new StatusBarResource(PATH_BASE + JAVASCRIPT);
        //((ResourceRegistry) FacesContext.getCurrentInstance())
        //	.loadJavascriptCode(jsResource);
        JavascriptContext.includeLib("./js/status_bar.js", FacesContext.getCurrentInstance());
    }

    private void registerImages() {
        JarResource bgImageResource = new StatusBarResource(PATH_BASE + BG_IMAGE);
        bgImagePath = ((ResourceRegistry) FacesContext.getCurrentInstance()).registerResource(
                bgImageResource).getPath();
        JarResource leftImageResource = new StatusBarResource(PATH_BASE + LEFT_IMAGE);
        leftImagePath = ((ResourceRegistry) FacesContext.getCurrentInstance()).registerResource(
                leftImageResource).getPath();
        JarResource rightImageResource = new StatusBarResource(PATH_BASE + RIGHT_IMAGE);
        rightImagePath = ((ResourceRegistry) FacesContext.getCurrentInstance()).registerResource(
                rightImageResource).getPath();
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int len = 0;
        while ((len = input.read(buf)) > -1)
            output.write(buf, 0, len);
        return output.toByteArray();
    }

    class StatusBarResource extends JarResource {

        public StatusBarResource(String path) {
            super(path);
        }

        public void withOptions(Options options) throws IOException {
        }
    }


}
