/*
 *  This class is part of the WebofThings project.
 *  http://www.webofthings.com/energievisible/
 *  (c) Dominique Guinard (www.guinard.org)
 *  Institute for Pervasive Computing, ETH Zurich
 *  and Cudrefin02.ch.
 */
package ch.unifr.diuf.softeng.client.observer.widgets;

import ch.unifr.diuf.softeng.client.config.Config;
import ch.unifr.diuf.softeng.client.config.EPC;
import ch.unifr.diuf.softeng.client.model.RestfulEpcisJsonEvent;
import ch.unifr.diuf.softeng.client.subject.ConcreteSubject;
import ch.unifr.diuf.softeng.client.subject.Subject;
import com.google.gwt.http.client.URL;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ContentsType;
import com.smartgwt.client.types.SendMethod;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.layout.VLayout;
import java.util.HashMap;
import java.util.Map;

/**
 * This Widget is searching Twitter according to the current product name.
 * @author <a href="http://www.guinard.org">Dominique Guinard</a>
 */
public class TwitterSearchWidget extends Widget {

    public static final String image = "set3/twitter.png";
    private VLayout layout;
    private String epc = "";
    private HTMLPane twitterPane;

    public TwitterSearchWidget(String title, int width, int height) {
        super(title, width, height, image);
        subscribeForEPC(this);
    }

    @Override
    public void initialize() {
        /* create an HTML pane to contain the Twitter Widget. */
        layout = new VLayout();
        layout.setMaxWidth(width);
        layout.setMaxHeight(height);

        twitterPane = new HTMLPane();
        twitterPane.setHeight("450px");
        twitterPane.setWidth("700px");
        twitterPane.setAlign(Alignment.CENTER);
        /* the ContentsType.PAGE parameters ensures that the pane is an IFRAME,
         * i.e. a new browser window, bypassing the same policy origin!
         */
        twitterPane.setContentsType(ContentsType.PAGE);

        /* add the HTML page to the main layout */
        layout.addMember(twitterPane);
        super.addMember(layout);

        //initialize data from subject
        String myEPC = ConcreteSubject.GET_INSTANCE().getEPC();
        if (!myEPC.equals("")) {
            this.update(myEPC, Subject.EPC, ConcreteSubject.GET_INSTANCE().getEPCData());
        }
    }

    @Override
    public void updateEPC(String state, final RestfulEpcisJsonEvent data) {
        epc = state;
        EPC currentEPC = Config.getEPC(epc);
        if (currentEPC != null) {
            Map params = new HashMap();
            twitterPane.setContentsURLParams(params);
            twitterPane.setHttpMethod(SendMethod.GET);
            String baseUrl = com.google.gwt.user.client.Window.Location.getHref();
            String requestUrl = baseUrl + "TwitterWidgetServer?query=" + currentEPC.getDescription();

            /* we need to encode that URL in order to escape chars such as spaces */
            String encodedUrl = URL.encode(requestUrl);
            twitterPane.setContentsURL(encodedUrl);
            twitterPane.draw();
        }
    }

}

