/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.myfaces.tobago.taglib.sandbox;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.apache.myfaces.tobago.TobagoConstants.FACET_LAYOUT;
import org.apache.myfaces.tobago.apt.annotation.ExtensionTag;
import org.apache.myfaces.tobago.apt.annotation.Tag;
import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
import org.apache.myfaces.tobago.apt.annotation.UIComponentTagAttribute;
import org.apache.myfaces.tobago.model.Wizard;
import org.apache.myfaces.tobago.model.WizardStep;
import org.apache.myfaces.tobago.taglib.component.AttributeTag;
import org.apache.myfaces.tobago.taglib.component.ButtonTag;
import org.apache.myfaces.tobago.taglib.component.GridLayoutTag;
import org.apache.myfaces.tobago.taglib.component.OutTag;
import org.apache.myfaces.tobago.taglib.component.PanelTag;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.webapp.FacetTag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.util.List;

@Tag(name = "wizardTrain")
@ExtensionTag(baseClassName = "org.apache.myfaces.tobago.taglib.sandbox.WizardTrainTag")
public class WizardTrainTag extends BodyTagSupport {

    private static final Log LOG = LogFactory.getLog(WizardTrainTag.class);

    private String controller;
    private String wizard;

    @Override
    public int doStartTag() throws JspException {

        int result = super.doStartTag();

        // XXX is this possible with facelets?
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        Wizard wizardObject = (Wizard) application.createValueBinding(controller).getValue(facesContext);
        List<WizardStep> course = wizardObject.getCourse();

        PanelTag panel = new PanelTag();
        panel.setPageContext(pageContext);
        panel.setParent(getParent());
        panel.doStartTag();

        FacetTag facetTag = new FacetTag();
        facetTag.setPageContext(pageContext);
        facetTag.setName(FACET_LAYOUT);
        facetTag.setParent(panel);
        facetTag.doStartTag();

        GridLayoutTag gridLayoutTag = new GridLayoutTag();
        gridLayoutTag.setPageContext(pageContext);
        StringBuilder columns = new StringBuilder();
        for (WizardStep info : course) {
            columns.append("fixed;");
        }
        gridLayoutTag.setColumns(columns + "*");
        gridLayoutTag.setParent(facetTag);
        gridLayoutTag.doStartTag();
        gridLayoutTag.doEndTag();

        facetTag.doEndTag();

        for (WizardStep info : course) {
            ButtonTag button = new ButtonTag();
            button.setPageContext(pageContext);
            button.setParent(panel);
            button.setAction(info.getOutcome());
            button.setActionListener("#{" + wizard + ".gotoStep}");
            button.setLabel(info.getTitle());
            button.doStartTag();

            AttributeTag step = new AttributeTag();
            step.setPageContext(pageContext);
            step.setParent(button);
            step.setName("step");
            step.setValue("" + info.getIndex());
            step.doStartTag();
            step.doEndTag();

            button.doEndTag();
        }
        OutTag spacer = new OutTag();
        spacer.setPageContext(pageContext);
        spacer.setParent(panel);
        spacer.setValue("#{" + wizard + ".index}");
        spacer.doStartTag();
        spacer.doEndTag();

        panel.doEndTag();

        return result;
    }

    @Override
    public void release() {
        super.release();
        controller = null;
        wizard = null;
    }

    @TagAttribute(required = true)
    @UIComponentTagAttribute
    public void setWizard(String wizard) {
        this.wizard = wizard;
    }

    @TagAttribute(required = true)
    @UIComponentTagAttribute
    public void setController(String controller) {
        this.controller = controller;
    }
}
