// Apps/DemoApp/UltraCanvasDropDownExamples.cpp
// Comprehensive dropdown/combobox examples with icons and multi-selection
// Version: 2.0.1
// Last Modified: 2026-05-01
// Author: UltraCanvas Framework

#include "UltraCanvasDemo.h"
#include "UltraCanvasDropdown.h"
#include "UltraCanvasLabel.h"
#include "UltraCanvasContainer.h"
#include "UltraCanvasButton.h"
#include <sstream>
#include <iostream>
#include "UltraCanvasDebug.h"

namespace UltraCanvas {

// Helper function to create section separator
    std::shared_ptr<UltraCanvasContainer> CreateDropdownSeparator(long x, long y, long width) {
        auto separator = std::make_shared<UltraCanvasContainer>("DropdownSep" + std::to_string(x), x, y, width, 2);
        separator->SetBackgroundColor(Color(200, 200, 200, 255));
        return separator;
    }

// Helper function to create section title
    std::shared_ptr<UltraCanvasLabel> CreateDropdownSectionTitle(long x, long y, const std::string& text) {
        auto title = std::make_shared<UltraCanvasLabel>("DropdownSecTitle" + std::to_string(x), x, y, 400, 25);
        title->SetText(text);
        title->SetFontSize(14);
        title->SetFontWeight(FontWeight::Bold);
        title->SetTextColor(Color(50, 50, 150, 255));
        return title;
    }

    std::shared_ptr<UltraCanvasUIElement> UltraCanvasDemoApplication::CreateDropdownExamples() {
        auto mainContainer = std::make_shared<UltraCanvasContainer>("DropdownExamples", 0, 0, 1020, 1000);
        mainContainer->SetBackgroundColor(Colors::White);
        mainContainer->SetPadding(0,0,10,0);

        long currentY = 10;

        // ===== MAIN TITLE =====
        auto mainTitle = std::make_shared<UltraCanvasLabel>("DropdownMainTitle", 20, currentY, 900, 30);
        mainTitle->SetText("UltraCanvas Dropdown Component Examples");
        mainTitle->SetFontSize(18);
        mainTitle->SetFontWeight(FontWeight::Bold);
        mainContainer->AddChild(mainTitle);

        currentY += 40;

        // Description
        auto description = std::make_shared<UltraCanvasLabel>("DropdownDesc", 20, currentY, 960, 40);
        description->SetText("Comprehensive dropdown examples showcasing icons, multi-selection, and various configurations");
        description->SetWrap(TextWrap::WrapWord);
        description->SetTextColor(Color(80, 80, 80, 255));
        mainContainer->AddChild(description);

        currentY += 30;

        // ===== SECTION 1: BASIC DROPDOWNS =====
        mainContainer->AddChild(CreateDropdownSectionTitle(20, currentY, "Basic Dropdowns"));
        currentY += 35;

        // Simple Dropdown
        auto simpleDropdown = std::make_shared<UltraCanvasDropdown>("SimpleDropdown", 30, currentY, 220, 30);
        simpleDropdown->AddItem("Option 1");
        simpleDropdown->AddItem("Option 2");
        simpleDropdown->AddItem("Option 3");
        simpleDropdown->AddItem("Very Long Option Text That Extends");
        simpleDropdown->SetSelectedIndex(0);

        auto simpleLabel = std::make_shared<UltraCanvasLabel>("SimpleLabel", 270, currentY + 5, 300, 20);
        simpleLabel->SetText("Simple dropdown with text items");
        simpleLabel->SetFontSize(12);

        mainContainer->AddChild(simpleDropdown);
        mainContainer->AddChild(simpleLabel);

        currentY += 45;

        // Dropdown with separators
        auto separatorDropdown = std::make_shared<UltraCanvasDropdown>("SeparatorDropdown", 30, currentY, 220, 30);
        separatorDropdown->AddItem("File Operations");
        separatorDropdown->AddItem("Open", "open");
        separatorDropdown->AddItem("Save", "save");
        separatorDropdown->AddSeparator();
        separatorDropdown->AddItem("Edit Operations");
        separatorDropdown->AddItem("Copy", "copy");
        separatorDropdown->AddItem("Paste", "paste");
        separatorDropdown->SetSelectedIndex(1);

        auto separatorLabel = std::make_shared<UltraCanvasLabel>("SeparatorLabel", 270, currentY + 5, 300, 20);
        separatorLabel->SetText("Dropdown with separators");
        separatorLabel->SetFontSize(12);

        mainContainer->AddChild(separatorDropdown);
        mainContainer->AddChild(separatorLabel);

        currentY += 45;
        mainContainer->AddChild(CreateDropdownSeparator(20, currentY, 960));
        currentY += 20;

        // ===== SECTION 2: DROPDOWNS WITH ICONS =====
        mainContainer->AddChild(CreateDropdownSectionTitle(20, currentY, "Dropdowns with Icons"));
        currentY += 35;

        // File types dropdown with icons
        auto fileTypesDropdown = std::make_shared<UltraCanvasDropdown>("FileTypesDropdown", 30, currentY, 220, 30);
        fileTypesDropdown->AddItem("Text Document", "txt", NormalizePath(GetResourcesDir() + "media/icons/document.png"));
        fileTypesDropdown->AddItem("PDF Document", "pdf", NormalizePath(GetResourcesDir() + "media/icons/pdf.png"));
        fileTypesDropdown->AddItem("Image File", "img", NormalizePath(GetResourcesDir() + "media/icons/image.png"));
        fileTypesDropdown->AddItem("Video File", "vid", NormalizePath(GetResourcesDir() + "media/icons/video.png"));
        fileTypesDropdown->SetSelectedIndex(0);

        auto fileTypesLabel = std::make_shared<UltraCanvasLabel>("FileTypesLabel", 270, currentY + 5, 250, 20);
        fileTypesLabel->SetText("File types with document icons");
        fileTypesLabel->SetFontSize(12);

        mainContainer->AddChild(fileTypesDropdown);
        mainContainer->AddChild(fileTypesLabel);

        currentY += 45;

        // Actions dropdown with icons
        auto actionsDropdown = std::make_shared<UltraCanvasDropdown>("ActionsDropdown", 30, currentY, 220, 30);
        actionsDropdown->AddItem("New File", "new", NormalizePath(GetResourcesDir() + "media/icons/new.png"));
        actionsDropdown->AddItem("Open File", "open", NormalizePath(GetResourcesDir() + "media/icons/folder.png"));
        actionsDropdown->AddItem("Save File", "save", NormalizePath(GetResourcesDir() + "media/icons/save.png"));
        actionsDropdown->AddItem("Print", "print", NormalizePath(GetResourcesDir() + "media/icons/print.png"));
        actionsDropdown->AddItem("Settings", "settings", NormalizePath(GetResourcesDir() + "media/icons/settings.png"));
        actionsDropdown->SetSelectedIndex(0);

        auto actionsLabel = std::make_shared<UltraCanvasLabel>("ActionsLabel", 270, currentY + 5, 250, 20);
        actionsLabel->SetText("Actions with icon indicators");
        actionsLabel->SetFontSize(12);

        mainContainer->AddChild(actionsDropdown);
        mainContainer->AddChild(actionsLabel);

        currentY += 45;
        mainContainer->AddChild(CreateDropdownSeparator(20, currentY, 960));
        currentY += 20;

        // ===== SECTION 3: COUNTRY FLAGS DROPDOWN =====
        mainContainer->AddChild(CreateDropdownSectionTitle(20, currentY, "Country Selection with Flags"));
        currentY += 35;

        // Countries dropdown with flags
        auto countriesDropdown = std::make_shared<UltraCanvasDropdown>("CountriesDropdown", 30, currentY, 280, 30);

        // Add countries with their flag icons (ISO 3166-1 alpha-2 codes)
        countriesDropdown->AddItem("United States", "US", NormalizePath(GetResourcesDir() + "media/flags/US.png"));
        countriesDropdown->AddItem("United Kingdom", "GB", NormalizePath(GetResourcesDir() + "media/flags/GB.png"));
        countriesDropdown->AddItem("Germany", "DE", NormalizePath(GetResourcesDir() + "media/flags/DE.png"));
        countriesDropdown->AddItem("France", "FR", NormalizePath(GetResourcesDir() + "media/flags/FR.png"));
        countriesDropdown->AddItem("Italy", "IT", NormalizePath(GetResourcesDir() + "media/flags/IT.png"));
        countriesDropdown->AddItem("Spain", "ES", NormalizePath(GetResourcesDir() + "media/flags/ES.png"));
        countriesDropdown->AddItem("Japan", "JP", NormalizePath(GetResourcesDir() + "media/flags/JP.png"));
        countriesDropdown->AddItem("China", "CN", NormalizePath(GetResourcesDir() + "media/flags/CN.png"));
        countriesDropdown->AddItem("India", "IN", NormalizePath(GetResourcesDir() + "media/flags/IN.png"));
        countriesDropdown->AddItem("Brazil", "BR", NormalizePath(GetResourcesDir() + "media/flags/BR.png"));
        countriesDropdown->AddItem("Canada", "CA", NormalizePath(GetResourcesDir() + "media/flags/CA.png"));
        countriesDropdown->AddItem("Australia", "AU", NormalizePath(GetResourcesDir() + "media/flags/AU.png"));
        countriesDropdown->AddItem("Mexico", "MX", NormalizePath(GetResourcesDir() + "media/flags/MX.png"));
        countriesDropdown->AddItem("Russia", "RU", NormalizePath(GetResourcesDir() + "media/flags/RU.png"));
        countriesDropdown->AddItem("South Korea", "KR", NormalizePath(GetResourcesDir() + "media/flags/KR.png"));
        countriesDropdown->AddItem("Netherlands", "NL", NormalizePath(GetResourcesDir() + "media/flags/NL.png"));
        countriesDropdown->AddItem("Sweden", "SE", NormalizePath(GetResourcesDir() + "media/flags/SE.png"));
        countriesDropdown->AddItem("Switzerland", "CH", NormalizePath(GetResourcesDir() + "media/flags/CH.png"));
        countriesDropdown->AddItem("Poland", "PL", NormalizePath(GetResourcesDir() + "media/flags/PL.png"));
        countriesDropdown->AddItem("Belgium", "BE", NormalizePath(GetResourcesDir() + "media/flags/BE.png"));

        countriesDropdown->SetSelectedIndex(0);

        // Country selection status label
        auto countryStatusLabel = std::make_shared<UltraCanvasLabel>("CountryStatus", 330, currentY + 5, 400, 20);
        countryStatusLabel->SetText("Selected: United States (US)");
        countryStatusLabel->SetFontSize(12);
        countryStatusLabel->SetTextColor(Color(0, 100, 0, 255));

        // Callback for country selection
        countriesDropdown->onSelectionChanged = [countryStatusLabel](int index, const DropdownItem& item) {
            debugOutput << "Country selected: " << item.text << " (" << item.value << ")" << std::endl;
            countryStatusLabel->SetText("Selected: " + item.text + " (" + item.value + ")");
        };

        mainContainer->AddChild(countriesDropdown);
        mainContainer->AddChild(countryStatusLabel);

        currentY += 45;
        mainContainer->AddChild(CreateDropdownSeparator(20, currentY, 960));
        currentY += 20;

        // ===== SECTION 4: MULTI-SELECTION DROPDOWNS =====
        mainContainer->AddChild(CreateDropdownSectionTitle(20, currentY, "Multi-Selection Dropdowns"));
        currentY += 35;

        // Programming languages multi-select
        auto languagesDropdown = std::make_shared<UltraCanvasDropdown>("LanguagesDropdown", 30, currentY, 250, 30);
        languagesDropdown->SetMultiSelectEnabled(true);
        languagesDropdown->AddItem("C++", "cpp", NormalizePath(GetResourcesDir() + "media/icons/cpp.png"));
        languagesDropdown->AddItem("Python", "py", NormalizePath(GetResourcesDir() + "media/icons/python.png"));
        languagesDropdown->AddItem("JavaScript", "js", NormalizePath(GetResourcesDir() + "media/icons/javascript.png"));
        languagesDropdown->AddItem("Java", "java", NormalizePath(GetResourcesDir() + "media/icons/java.png"));
        languagesDropdown->AddItem("C#", "cs", NormalizePath(GetResourcesDir() + "media/icons/csharp.png"));
        languagesDropdown->AddItem("Go", "go", NormalizePath(GetResourcesDir() + "media/icons/go.png"));

        // Pre-select some items
        languagesDropdown->SetItemSelected(0, true); // C++
        languagesDropdown->SetItemSelected(1, true); // Python

        auto languagesStatus = std::make_shared<UltraCanvasLabel>("LanguagesStatus", 300, currentY + 5, 400, 60);
        languagesStatus->SetText("Selected: C++, Python");
        languagesStatus->SetWrap(TextWrap::WrapWord);
        languagesStatus->SetFontSize(12);
        languagesStatus->SetTextColor(Color(0, 100, 0, 255));

        // Callback for multi-selection
        languagesDropdown->onSelectedItemsChanged = [languagesStatus](const std::vector<DropdownItem>& items) {
            std::string selectedText = "Selected: ";
            if (items.empty()) {
                selectedText += "None";
            } else {
                for (size_t i = 0; i < items.size(); ++i) {
                    selectedText += items[i].text;
                    if (i < items.size() - 1) selectedText += ", ";
                }
            }
            languagesStatus->SetText(selectedText);
            debugOutput << "Programming languages selection changed: " << selectedText << std::endl;
        };

        mainContainer->AddChild(languagesDropdown);
        mainContainer->AddChild(languagesStatus);

        currentY += 75;

        // Fruit multi-select (no icons)
        auto fruitsDropdown = std::make_shared<UltraCanvasDropdown>("FruitsDropdown", 30, currentY, 250, 30);
        fruitsDropdown->SetMultiSelectEnabled(true);
        fruitsDropdown->AddItem("Apple", "apple");
        fruitsDropdown->AddItem("Banana", "banana");
        fruitsDropdown->AddItem("Orange", "orange");
        fruitsDropdown->AddItem("Grape", "grape");
        fruitsDropdown->AddItem("Strawberry", "strawberry");
        fruitsDropdown->AddItem("Mango", "mango");
        fruitsDropdown->AddItem("Pineapple", "pineapple");
        fruitsDropdown->AddItem("Watermelon", "watermelon");

        auto fruitsLabel = std::make_shared<UltraCanvasLabel>("FruitsLabel", 300, currentY + 5, 400, 20);
        fruitsLabel->SetText("Multi-select fruits (with checkboxes)");
        fruitsLabel->SetFontSize(12);

        mainContainer->AddChild(fruitsDropdown);
        mainContainer->AddChild(fruitsLabel);

        currentY += 45;

        // Control buttons for fruits dropdown
        auto selectAllBtn = std::make_shared<UltraCanvasButton>("SelectAllFruits", 30, currentY, 100, 30);
        selectAllBtn->SetText("Select All");
        selectAllBtn->SetOnClick([fruitsDropdown]() {
            fruitsDropdown->SelectAll();
            debugOutput << "Selected all fruits" << std::endl;
        });

        auto deselectAllBtn = std::make_shared<UltraCanvasButton>("DeselectAllFruits", 140, currentY, 100, 30);
        deselectAllBtn->SetText("Clear All");
        deselectAllBtn->SetOnClick([fruitsDropdown]() {
            fruitsDropdown->DeselectAll();
            debugOutput << "Cleared all fruit selections" << std::endl;
        });

        auto getSelectedBtn = std::make_shared<UltraCanvasButton>("GetSelectedFruits", 250, currentY, 120, 30);
        getSelectedBtn->SetText("Get Selected");
        getSelectedBtn->SetOnClick([fruitsDropdown]() {
            auto selected = fruitsDropdown->GetSelectedItems();
            debugOutput << "Currently selected fruits (" << selected.size() << "): ";
            for (const auto& item : selected) {
                debugOutput << item.text << " ";
            }
            debugOutput << std::endl;
        });

        mainContainer->AddChild(selectAllBtn);
        mainContainer->AddChild(deselectAllBtn);
        mainContainer->AddChild(getSelectedBtn);

        currentY += 45;
        mainContainer->AddChild(CreateDropdownSeparator(20, currentY, 960));
        currentY += 20;

        // ===== SECTION 5: MULTI-SELECT COUNTRIES WITH FLAGS =====
        mainContainer->AddChild(CreateDropdownSectionTitle(20, currentY, "Multi-Select Countries (Travel Destinations)"));
        currentY += 35;

        // Travel destinations multi-select with flags
        auto travelDropdown = std::make_shared<UltraCanvasDropdown>("TravelDropdown", 30, currentY, 280, 30);
        travelDropdown->SetMultiSelectEnabled(true);
        travelDropdown->AddItem("France", "FR", NormalizePath(GetResourcesDir() + "media/flags/FR.png"));
        travelDropdown->AddItem("Italy", "IT", NormalizePath(GetResourcesDir() + "media/flags/IT.png"));
        travelDropdown->AddItem("Spain", "ES", NormalizePath(GetResourcesDir() + "media/flags/ES.png"));
        travelDropdown->AddItem("Japan", "JP", NormalizePath(GetResourcesDir() + "media/flags/JP.png"));
        travelDropdown->AddItem("Thailand", "TH", NormalizePath(GetResourcesDir() + "media/flags/TH.png"));
        travelDropdown->AddItem("United States", "US", NormalizePath(GetResourcesDir() + "media/flags/US.png"));
        travelDropdown->AddItem("United Kingdom", "GB", NormalizePath(GetResourcesDir() + "media/flags/GB.png"));
        travelDropdown->AddItem("Australia", "AU", NormalizePath(GetResourcesDir() + "media/flags/AU.png"));
        travelDropdown->AddItem("Greece", "GR", NormalizePath(GetResourcesDir() + "media/flags/GR.png"));
        travelDropdown->AddItem("Switzerland", "CH", NormalizePath(GetResourcesDir() + "media/flags/CH.png"));

        auto travelStatus = std::make_shared<UltraCanvasLabel>("TravelStatus", 330, currentY + 5, 500, 60);
        travelStatus->SetText("Select your travel destinations");
        travelStatus->SetWrap(TextWrap::WrapWord);
        travelStatus->SetFontSize(12);
        travelStatus->SetBackgroundColor(Color(250, 250, 250, 255));
        travelStatus->SetBorders(1, Color(200, 200, 200, 255));
        travelStatus->SetPadding(5);

        // Callback for travel selection
        travelDropdown->onMultiSelectionChanged = [travelStatus](const std::vector<int>& indices) {
            std::string statusText;
            if (indices.empty()) {
                statusText = "No destinations selected yet";
            } else {
                statusText = "Selected " + std::to_string(indices.size()) + " destination";
                if (indices.size() > 1) statusText += "s";
            }
            travelStatus->SetText(statusText);
        };

        travelDropdown->onSelectedItemsChanged = [](const std::vector<DropdownItem>& items) {
            debugOutput << "Travel destinations: ";
            for (const auto& item : items) {
                debugOutput << item.text << " (" << item.value << ") ";
            }
            debugOutput << std::endl;
        };

        mainContainer->AddChild(travelDropdown);
        mainContainer->AddChild(travelStatus);

        currentY += 75;
        mainContainer->AddChild(CreateDropdownSeparator(20, currentY, 960));
        currentY += 20;

        // ===== SECTION 6: INTERACTIVE DEMO =====
        mainContainer->AddChild(CreateDropdownSectionTitle(20, currentY, "Interactive Demo"));
        currentY += 35;

        // Create interactive demo with event logging
        auto interactiveDropdown = std::make_shared<UltraCanvasDropdown>("InteractiveDropdown", 30, currentY, 250, 30);
        interactiveDropdown->AddItem("Monitor Events", "monitor");
        interactiveDropdown->AddItem("Selection Changed", "selection");
        interactiveDropdown->AddItem("Item Hovered", "hover");
        interactiveDropdown->AddItem("Dropdown Opened", "opened");
        interactiveDropdown->AddItem("Dropdown Closed", "closed");
        interactiveDropdown->SetSelectedIndex(0);

        auto eventLog = std::make_shared<UltraCanvasLabel>("EventLog", 300, currentY, 500, 80);
        eventLog->SetText("Event log: No events yet");
        eventLog->SetWrap(TextWrap::WrapWord);
        eventLog->SetFontSize(11);
        eventLog->SetBackgroundColor(Color(245, 245, 245, 255));
        eventLog->SetBorders(1.0f);
        eventLog->SetBordersColor(Color(180, 180, 180, 255));
        eventLog->SetPadding(5.0f);

        // Set up all callbacks
        interactiveDropdown->onSelectionChanged = [eventLog](int index, const DropdownItem& item) {
            std::string log = "Selection Changed: " + item.text + " (index: " + std::to_string(index) + ")";
            eventLog->SetText(log);
            debugOutput << log << std::endl;
        };

        interactiveDropdown->onItemHovered = [eventLog](int index, const DropdownItem& item) {
            std::string log = "Item Hovered: " + item.text + " (index: " + std::to_string(index) + ")";
            eventLog->SetText(log);
        };

        interactiveDropdown->onDropdownOpened = [eventLog]() {
            std::string log = "Event: Dropdown Opened";
            eventLog->SetText(log);
            debugOutput << log << std::endl;
        };

        interactiveDropdown->onDropdownClosed = [eventLog]() {
            std::string log = "Event: Dropdown Closed";
            eventLog->SetText(log);
            debugOutput << log << std::endl;
        };

        mainContainer->AddChild(interactiveDropdown);
        mainContainer->AddChild(eventLog);

        currentY += 95;

        debugOutput << "Dropdown examples created successfully with " << (currentY + 60) << " pixels height" << std::endl;

        return mainContainer;
    }

} // namespace UltraCanvas