package com.atlassian.stash.internal.plugin.web.fragments;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.hostcontainer.HostContainer;
import com.atlassian.plugin.module.ModuleFactory;
import com.atlassian.plugin.web.WebFragmentHelper;
import com.atlassian.plugin.web.descriptors.ConditionElementParser;
import org.dom4j.Element;

import static com.atlassian.stash.internal.plugin.web.fragments.ClientWebFragmentFieldType.TEXT;

public class ClientWebItemModuleDescriptor extends AbstractClientWebFragmentModuleDescriptor {

    public static final String XML_ELEMENT_NAME = "client-web-item".intern();

    private String location;

    public ClientWebItemModuleDescriptor(ModuleFactory moduleFactory, HostContainer hostContainer,
                                         ClientWebInterfaceManager clientWebInterfaceManager,
                                         ConditionElementParser.ConditionFactory conditionFactory,
                                         WebFragmentHelper webFragmentHelper) {
        super(moduleFactory, hostContainer, clientWebInterfaceManager, conditionFactory, webFragmentHelper);
    }

    @Override
    protected ClientWebFragmentType getFragmentType() {
        return ClientWebFragmentType.ITEM;
    }

    @Override
    protected void addFields(Element container, Element originalElement) {
        copyElement("tooltip", originalElement, container, TEXT);
        copyElement("styleClass", originalElement, container, TEXT);
        copyElement("description", originalElement, container, TEXT);

        location = originalElement.attributeValue("section");
        if (location == null) {
            throw new PluginParseException("section is a required attribute for client web items.");
        }
        addStringElement(container, "location", location);

        Element link = originalElement.element("link");
        if (link != null) {
            String linkId = link.attributeValue("linkId");
            if (linkId != null) {
                addTextElement(container, "linkId", linkId);
            }
            addElement(container, "url", getType(link, TEXT), link.getText(), link.attributeValue("key"));
        }

        Element label = originalElement.element("label");
        if (label != null) {
            addElement(container, "linkText", getType(label, TEXT), label.getText(), label.attributeValue("key"));
        }

        Element icon = originalElement.element("icon");
        if (icon != null) {
            String height = icon.attributeValue("height");
            if (height != null) {
                addNumberElement(container, "iconHeight", Float.parseFloat(height));
            }
            String width = icon.attributeValue("width");
            if (width != null) {
                addNumberElement(container, "iconWidth", Float.parseFloat(width));
            }
            Element iconLink = icon.element("link");
            addElement(container, "iconUrl", getType(iconLink, TEXT), iconLink.getText(), iconLink.attributeValue("key"));
        }
    }

    @Override
    public String getLocation() {
        return location;
    }
}
