/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.patterns.UNIVERSAL.CLASS.Coad_Classes.Description;

import java.util.Vector;

import com.togethersoft.openapi.sci.SciClass;
import com.togethersoft.openapi.sci.SciElement;
import com.togethersoft.openapi.sci.SciProperty;

import com.togethersoft.openapi.sci.pattern.SciPattern;
import com.togethersoft.openapi.sci.pattern.SciPatternUtil;
import com.togethersoft.openapi.sci.pattern.SciPatternProperty;
import com.togethersoft.openapi.sci.pattern.SciPatternCategory;

import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageType;

import com.togethersoft.modules.patterns.UNIVERSAL.CLASS.ClassPatternBase;
import com.togethersoft.modules.patterns.UNIVERSAL.PatternBaseException;
import com.togethersoft.modules.patterns.UNIVERSAL.PatternUIBuilder;

/**
 * Description pattern from ArchetypesInColor
 * @author TogetherSoft
 */
public class Pattern extends ClassPatternBase implements SciPattern {

    private static final String PATTERN_NAME = "Description";

    public Pattern() {
        setPatternDisplayName(PATTERN_NAME);
    }

    protected void finalize() throws Throwable {
        super.finalize();
        if (myUIBuilder != null) {
            myUIBuilder.removeAllListenedInspectorProperties();
            myUIBuilder = null;
        }
        myDescriptionClass = null;
        myAssessClass = null;
    }

    public boolean prepare() {
        try {
            super.prepare();
            notImplementedForIDL();

            SciElement testElement = getElement();
            if (testElement instanceof SciClass) {
                setPropertyValue(DESCRIPTION_NAME, testElement.getName());
                myDescriptionClass = (SciClass)testElement;
            }
            else {
                setPropertyValue(DESCRIPTION_NAME, DESCRIPTION);
                myDescriptionClass = null;
            }
            setPropertyValue(ASSESS_INTERFACE_NAME, IASSESS);
            myAssessClass = null;

            initializeInspector();
            assignActivityResponse();
            return true;
        }
        catch(PatternBaseException e) {
            return false;
        }
    }

    public boolean canApply() {
        try {
            String descriptionName = classNameShouldBeCorrect(DESCRIPTION_NAME);
            String assessName = classNameShouldBeCorrect(ASSESS_INTERFACE_NAME);
            if (descriptionName.equals(assessName)) {
                throw new PatternBaseException("Names should be different");
            }
            if (myDescriptionClass != null && !myDescriptionClass.canSetName(descriptionName)) {
                throw new PatternBaseException("Cannot rename class to " + descriptionName);
            }
            return true;
        }
        catch(PatternBaseException e) {
            return false;
        }
    }

    public void apply() {
        if (!canApply()) {
            return;
        }
        super.apply();

        if (myUIBuilder != null) {
            myUIBuilder.removeAllListenedInspectorProperties();
            myUIBuilder = null;
        }

        String descriptionName = getStringPropertyValue(DESCRIPTION_NAME);
        String assessName = getStringPropertyValue(ASSESS_INTERFACE_NAME);
        if (myDescriptionClass != null) {
            myDescriptionClass.setName(descriptionName);
            if (!myDescriptionClass.getName().equals(descriptionName)) {
                IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR, "Couldn't rename class.");
            }
        }
        else {
            myDescriptionClass = getClassByName(descriptionName, false);
        }

        myAssessClass = getClassByName(assessName, true);
        myAssessClass.getTagList().setTagValue(STEREOTYPE, STYPE_PLUG_IN_POINT);
        myDescriptionClass.getTagList().setTagValue(STEREOTYPE, STYPE_DESCRIPTION);

        pasteNewAttribute(myDescriptionClass, TYPE);
        pasteNewAttribute(myDescriptionClass, DESCRIPTION);
        pasteNewAttribute(myDescriptionClass, "itemNumber");
        pasteNewAttribute(myDescriptionClass, "defaultValue");

        // Create link
        SciElement  attribute = null;
        String      attrName = PREFIX_LINK + assessName;
        attribute = createLinkByPattern(DEFAULT_ASSOCIATION, myDescriptionClass, myAssessClass, attrName, false);
        if (attribute == null) {
            return;
        }
        attribute.getTagList().setTagValue(CARDINALITY_SUPPLIER, CARD_ZERO_ONE);

        pasteNewOperation(myDescriptionClass, "assessWithPlugInElseDefault");
        pasteNewOperation(myDescriptionClass, "assessAcrossPPTs");
        pasteNewOperation(myDescriptionClass, "findAvailable");
        pasteNewOperation(myDescriptionClass, "calcQtyAvailable");

        pasteNewOperation(myAssessClass, "assess");

        pasteNewOperation(myDescriptionClass, "calcTotalFor", INT);
        pasteNewOperation(myDescriptionClass, "listPPT", INT);
        pasteNewOperation(myDescriptionClass, "listDescs", INT).setProperty(SciProperty.STATIC, true);
        pasteNewOperation(myDescriptionClass, "assessAcrossDescs", INT).setProperty(SciProperty.STATIC, true);
    }

    protected void initializeInspector() {
        try {
            myUIBuilder = null;
            myUIBuilder = createUIBuilder(true);

            String pageName1 = "Pattern properties";
            if (myUIBuilder.addInspectorPage(pageName1, PatternUIBuilder.UIPresentation.Table) == null) {
                throw new PatternBaseException(CANNOT_CREATE_PATTERN_UI);
            }
            // Add description property
            if (myUIBuilder.addPropertyToPage(pageName1, DESCRIPTION_NAME, DESCRIPTION_NAME,
                PatternUIBuilder.PropertyType.String) == null) {
                throw new PatternBaseException(CANNOT_CREATE_PATTERN_UI);
            }
            // Add access interface property
            if (myUIBuilder.addPropertyToPage(pageName1, ASSESS_INTERFACE_NAME, ASSESS_INTERFACE_NAME,
                PatternUIBuilder.PropertyType.String) == null) {
                throw new PatternBaseException(CANNOT_CREATE_PATTERN_UI);
            }

            myUIBuilder.setCreatedPatternUI();

            Vector inspectorPropertiesVector = new Vector();
            inspectorPropertiesVector.addElement(DESCRIPTION_NAME);
            inspectorPropertiesVector.addElement(ASSESS_INTERFACE_NAME);
            myUIBuilder.assignListenedInspectorProperties(inspectorPropertiesVector);
        }
        catch (PatternBaseException ex) {
            myUIBuilder = null;
            getIdeInspector().clear();
            return;
        }
    }

    protected void assignActivityResponse() {
    }

    private PatternUIBuilder    myUIBuilder = null;
    private SciClass            myDescriptionClass = null;
    private SciClass            myAssessClass = null;

    private static final String DESCRIPTION_NAME        = "Description Name";
    private static final String ASSESS_INTERFACE_NAME   = "Assess Interface Name";
    private static final String DESCRIPTION             = "Description";
    private static final String TYPE                    = "type";
    private static final String IASSESS                 = "IAssess";
}
