package com.installshield.watchpoint.event.dialog.swing;


import java.util.Locale;

import com.installshield.event.*;
import com.installshield.event.ui.ISDialogExitContext;
import com.installshield.wizard.service.*;
import com.installshield.wizard.*;
import com.installshield.product.service.product.*;
import com.installshield.ui.controls.ISLocaleControl;
import com.installshield.util.*;
import com.installshield.util.sort.*;

public class PanelLocale {
    
    public void exitingLocale(ISDialogExitContext context) {
        ISLocaleControl localeCtrl = (ISLocaleControl) context.getISPanel()
                .getControl("locale_control");
        if (localeCtrl != null) {
            Locale[] selLocales = localeCtrl.getSelectedLocales();
            updateProductTree(selLocales, context.getServices());
        }
    }
    
    private void updateProductTree(Locale[] selLocales, WizardServices services) {
        String selectedLocales = LocaleUtils.encodeLocales(selLocales);
        try {
            ProductService service = (ProductService) services
                    .getService(ProductService.NAME);
            service.setProductTreeProperty(
                    ProductService.DEFAULT_PRODUCT_SOURCE, "selectedLocales",
                    selectedLocales);
        } catch (ServiceException e) {
        }
    }

    public void generateOptionsEntriesLocale(ISOptionsContext context) {

        try {
            ProductService pService =
                (ProductService)context.getService(ProductService.NAME);

            Locale[] locales = getAvailableLocales(pService);

            String title =
                LocalizedStringResolver.resolve(
                    "com.installshield.product.i18n.ProductResources",
                    "LocalePanel.ote1Title");
            StringBuffer enumStr = new StringBuffer();
            enumStr.append("<enum>");
            for (int i = 0; i < locales.length; i++) {
                enumStr.append("<value>");
                enumStr.append(
                    locales[i].toString()
                        + ":"
                        + LocaleUtils.getLocaleDisplayName(locales[i]));
                enumStr.append("</value>");
            }

            enumStr.append("</enum>");

            // assert locales.length > 0
            // Otherwise set no options entries for context
            if ((locales != null) && (locales.length > 0)) {
                String sampleLocale =
                    LocaleUtils.getLocaleDisplayName(locales[0]);
                String sampleOption =
                    "-P selectedLocales=" + locales[0].toString();
                String multMsg = "";

                if (locales.length > 1) {
                    multMsg =
                        LocalizedStringResolver.resolve(
                            "com.installshield.product.i18n.ProductResources",
                            "LocalePanel.ote1MultLocales",
                            new String[] {
                                locales[0].toString(),
                                locales[1].toString(),
                                LocaleUtils.getLocaleDisplayName(locales[0]),
                                LocaleUtils.getLocaleDisplayName(locales[1])});
                }

                String doc =
                    LocalizedStringResolver.resolve(
                        "com.installshield.product.i18n.ProductResources",
                        "LocalePanel.ote1Doc",
                        new String[] {
                            enumStr.toString(),
                            sampleLocale,
                            sampleOption,
                            multMsg });
                String option = "-P selectedLocales=";
                if (context.getValueType() == WizardBean.TEMPLATE_VALUE) {
                    option
                        += LocalizedStringResolver.resolve(
                            "com.installshield.wizard.i18n.WizardResources",
                            "WizardBean.valueStr");
                }
                else {
                    try {
                        String selectedLocales =
                            (String)pService.getProductTreeProperty(
                                ProductService.DEFAULT_PRODUCT_SOURCE,
                                "selectedLocales");
                        if (selectedLocales == null) {
                            selectedLocales = "";
                        }
                        option += selectedLocales;
                    }
                    catch (Exception e) {
                        LogUtils.getLog().logEvent(this, Log.ERROR, e);
                    }
                }

                context.getOptionEntries().addElement(
                    new OptionsTemplateEntry(title, doc, option));
            }
        }
        catch (ServiceException e) {
            LogUtils.getLog().logEvent(this, Log.ERROR, e);
        }

    }

    private Locale[] getAvailableLocales(ProductService pService) {

        Locale[] available = new Locale[0];

        try {
            String[] availableStrs =
                pService.getProductLocales(
                    ProductService.DEFAULT_PRODUCT_SOURCE);
            available = new Locale[availableStrs.length];
            for (int i = 0; i < availableStrs.length; i++) {
                available[i] = PropertyUtils.createLocale(availableStrs[i]);
            }
            SortUtils.qsort(available, new LocaleCompare());
        }
        catch (ServiceException e) {
            available = new Locale[0];
        }
        return available;
    }

}
