#include "GenerateCodeSketch.h"

#include <GG/GUI.h>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include <GG/Button.h>
#include <GG/DropDownList.h>
#include <GG/Edit.h>
#include <GG/Layout.h>
#include <GG/TextControl.h>

#include <string>
#include <fstream>


GenerateCodeSketch::GenerateCodeSketch(GG::Wnd* parent)
{
    GG::GUI* gui = GG::GUI::GetGUI();

    std::ifstream ifs("GenerateCodeSketch.sketch");
    boost::archive::xml_iarchive ia(ifs);
    GG::Layout* dialog_layout;
    int num_wnds;
    std::string name, type;
    std::set<GG::Wnd*> wnds;
    GG::Pt dialog_client_ul, dialog_client_lr;

    gui->LoadWnd(dialog_layout, "dialog_layout", ia);
    ia & BOOST_SERIALIZATION_NVP(dialog_client_ul)
       & BOOST_SERIALIZATION_NVP(dialog_client_lr)
       & BOOST_SERIALIZATION_NVP(num_wnds);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_custom_header_ext_edit, "m_custom_header_ext_edit", ia);
    wnds.insert(m_custom_header_ext_edit);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(Layout17, "Layout17", ia);
    wnds.insert(Layout17);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(Layout20, "Layout20", ia);
    wnds.insert(Layout20);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(Layout18, "Layout18", ia);
    wnds.insert(Layout18);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl8, "TextControl8", ia);
    wnds.insert(TextControl8);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl15, "TextControl15", ia);
    wnds.insert(TextControl15);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(Layout19, "Layout19", ia);
    wnds.insert(Layout19);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_impl_ext_list, "m_impl_ext_list", ia);
    wnds.insert(m_impl_ext_list);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_custom_impl_ext_edit, "m_custom_impl_ext_edit", ia);
    wnds.insert(m_custom_impl_ext_edit);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl9, "TextControl9", ia);
    wnds.insert(TextControl9);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_sketch_ext_list, "m_sketch_ext_list", ia);
    wnds.insert(m_sketch_ext_list);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_custom_sketch_ext_edit, "m_custom_sketch_ext_edit", ia);
    wnds.insert(m_custom_sketch_ext_edit);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl16, "TextControl16", ia);
    wnds.insert(TextControl16);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl10, "TextControl10", ia);
    wnds.insert(TextControl10);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl7, "TextControl7", ia);
    wnds.insert(TextControl7);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_header_ext_list, "m_header_ext_list", ia);
    wnds.insert(m_header_ext_list);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_export_button, "m_export_button", ia);
    wnds.insert(m_export_button);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(Layout27, "Layout27", ia);
    wnds.insert(Layout27);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_cancel_button, "m_cancel_button", ia);
    wnds.insert(m_cancel_button);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(RadioButtonGroupLayout4, "RadioButtonGroupLayout4", ia);
    wnds.insert(RadioButtonGroupLayout4);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_file_types_button_group, "m_file_types_button_group", ia);
    wnds.insert(m_file_types_button_group);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(StateButton0, "StateButton0", ia);
    wnds.insert(StateButton0);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(StateButton1, "StateButton1", ia);
    wnds.insert(StateButton1);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(StateButton2, "StateButton2", ia);
    wnds.insert(StateButton2);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl21, "TextControl21", ia);
    wnds.insert(TextControl21);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(m_base_filename, "m_base_filename", ia);
    wnds.insert(m_base_filename);
    ia & BOOST_SERIALIZATION_NVP(name) & BOOST_SERIALIZATION_NVP(type);
    gui->LoadWnd(TextControl24, "TextControl24", ia);
    wnds.insert(TextControl24);

    parent->SizeMove(dialog_client_ul - (parent->ClientUpperLeft() - parent->UpperLeft()),
                     dialog_client_lr + (parent->LowerRight() - parent->ClientLowerRight()));
    if (dialog_layout) {
        parent->SetLayout(dialog_layout);
    } else {
        for (std::set<GG::Wnd*>::const_iterator it = wnds.begin(); it != wnds.end(); ++it) {
            if (!(*it)->Parent() || wnds.find((*it)->Parent()) == wnds.end())
                parent->AttachChild(*it);
        }
    }
}
