package com.izforge.izpack.installer.container.impl;

import com.izforge.izpack.api.data.AutomatedInstallData;
import com.izforge.izpack.api.data.ResourceManager;
import com.izforge.izpack.api.exception.ResourceNotFoundException;
import com.izforge.izpack.api.substitutor.VariableSubstitutor;
import com.izforge.izpack.core.container.AbstractChildContainer;
import com.izforge.izpack.installer.base.InstallerFrame;
import com.izforge.izpack.installer.container.IInstallerContainer;
import com.izforge.izpack.installer.language.LanguageDialog;
import com.izforge.izpack.installer.manager.PanelManager;
import com.izforge.izpack.installer.unpacker.IUnpacker;
import org.picocontainer.parameters.ComponentParameter;
import org.picocontainer.parameters.ConstantParameter;

import javax.swing.*;
import java.awt.*;

/**
 * Container for panel level component
 */
public class InstallerContainer extends AbstractChildContainer implements IInstallerContainer
{

    public InstallerContainer(ApplicationContainer parent) throws ClassNotFoundException
    {
        super(parent);
        initBindings();
    }

    public void initBindings() throws ClassNotFoundException
    {
        pico
                .addComponent(IInstallerContainer.class, this)
                .addComponent(PanelManager.class)
                ;
        addVariablerComponent();
    }

    private void addVariablerComponent() throws ClassNotFoundException
    {
        AutomatedInstallData installdata = pico.getComponent(AutomatedInstallData.class);
        VariableSubstitutor substitutor = pico.getComponent(VariableSubstitutor.class);
        String unpackerclassname = installdata.getInfo().getUnpackerClassName();
        Class<IUnpacker> unpackerclass = (Class<IUnpacker>) Class.forName(unpackerclassname);
        pico
                .addComponent(IUnpacker.class, unpackerclass)
                .addComponent(InstallerFrame.class, InstallerFrame.class,
                        new ConstantParameter(getTitle(installdata, substitutor)),
                        new ComponentParameter(),
                        new ComponentParameter(),
                        new ComponentParameter(),
                        new ComponentParameter(),
                        new ComponentParameter(),
                        new ComponentParameter(),
                        new ComponentParameter(),
                        new ComponentParameter()
                )
                .addComponent(LanguageDialog.class, LanguageDialog.class,
                        new ConstantParameter(initFrame()),
                        new ComponentParameter(),
                        new ComponentParameter(),
                        new ComponentParameter()
                );
    }


    private JFrame initFrame()
    {
        ResourceManager resourceManager = pico.getComponent(ResourceManager.class);
        // Dummy Frame
        JFrame frame = new JFrame();
        ImageIcon imageIcon = null;
        try
        {
            imageIcon = resourceManager.getImageIconResource("JFrameIcon");
        }
        catch (ResourceNotFoundException e)
        {
            imageIcon = new ImageIcon(this.getClass().getResource("/img/JFrameIcon.png"));
        }
        frame.setIconImage(imageIcon.getImage());

        Dimension frameSize = frame.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2 - 10);
        return frame;
    }

    private String getTitle(AutomatedInstallData automatedInstallData, VariableSubstitutor vs)
    {
        // Use a alternate message if defined.
        final String key = "installer.reversetitle";
        String message = automatedInstallData.getLangpack().getString(key);
        // message equal to key -> no message defined.
        if (message.indexOf(key) > -1)
        {
            return automatedInstallData.getLangpack().getString("installer.title")
                    + automatedInstallData.getInfo().getAppName();
        }
        else
        { // Attention! The alternate message has to contain the whole message including
            // $APP_NAME and may be $APP_VER.
            return vs.substitute(message);
        }
    }


}

@@ -679,92 +679,23 @@
 ner.
-parameters.ComponentParameter;%0Aimport org.picocontainer.parameters.ConstantParameter
+Characteristics
 ;%0A%0Ai
@@ -1107,33 +1107,16 @@
     pico
-%0A                
 .addComp
@@ -1207,143 +1207,10 @@
 ass)
+;
 %0A
-                ;%0A        addVariablerComponent();%0A    %7D%0A%0A    private void addVariablerComponent() throws ClassNotFoundException%0A    %7B
 %0A   
@@ -1592,105 +1592,46 @@
     
-.addComponent(IUnpacker.class, unpackerclass)%0A                .addComponent(InstallerFrame.class,
+// Configuration of title parameter in
  Ins
@@ -1641,23 +1641,16 @@
 lerFrame
-.class,
 %0A       
@@ -1662,38 +1662,28 @@
     
-        new ConstantParameter(
+.addConfig(%22title%22, 
 getT
@@ -1713,17 +1713,16 @@
 itutor))
-,
 %0A       
@@ -1742,477 +1742,187 @@
     
-new ComponentParameter(),%0A                        new ComponentParameter(),%0A                        new ComponentParameter(),%0A                        new ComponentParameter(),%0A                        new ComponentParameter(),%0A                        new ComponentParameter(),%0A                        new ComponentParameter(),%0A                        new ComponentParameter()%0A                )%0A                .addComponent(LanguageDialog.class, LanguageDialog.
+// Configuration of frame parameter in languageDialog%0A                .addConfig(%22frame%22, initFrame());%0A        pico%0A                .addComponent(IUnpacker.class, unpacker
 class
-,%0A  
+)%0A
     
@@ -1937,215 +1937,154 @@
     
-      new ConstantParameter(initFrame()),%0A                        new ComponentParameter(),%0A                        new ComponentParameter(),%0A                        new ComponentParameter()%0A                
+.as(Characteristics.USE_NAMES).addComponent(InstallerFrame.class)%0A                .as(Characteristics.USE_NAMES).addComponent(LanguageDialog.class
 );%0A 

Use of configuration name instead of constant parameter for installer container