#include "SplashDialog.h"
#include "Version.h"
#include "ResourceLoader.h"
#include "Prefs.h"
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include <wx/image.h>
#include <wx/hyperlink.h>

wxBEGIN_EVENT_TABLE(SplashDialog, wxDialog)
    EVT_PAINT(SplashDialog::OnPaint)
    EVT_KEY_DOWN(SplashDialog::OnKeyDown)
wxEND_EVENT_TABLE()

SplashDialog::SplashDialog(wxWindow* parent)
    : wxDialog(parent, wxID_ANY, TD_APP_FULL_TITLE,
               wxDefaultPosition, wxSize(kImgW, kImgH + 100),
               wxDEFAULT_DIALOG_STYLE)
{
    wxInitAllImageHandlers();

    // Load images from embedded resources (not from disk)
    wxImage img = LoadImageResource(RES_SPLASH);
    if (img.IsOk())
        m_bitmap = wxBitmap(img.Scale(kImgW, kImgH, wxIMAGE_QUALITY_HIGH));

    wxBitmap cashierBmp;
    if (Prefs::Get().IsPlayful()) {
        wxImage cashierImg = LoadImageResource(RES_CASHIER);
        if (cashierImg.IsOk())
            cashierBmp = wxBitmap(cashierImg.Scale(120, 160, wxIMAGE_QUALITY_HIGH));
    }

    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    wxPanel* imgPanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(kImgW, kImgH));
    imgPanel->Bind(wxEVT_PAINT, [this, imgPanel, cashierBmp](wxPaintEvent&) {
        wxPaintDC dc(imgPanel);
        if (m_bitmap.IsOk())
            dc.DrawBitmap(m_bitmap, 0, 0, false);
        else {
            dc.SetBackground(wxBrush(wxColour(30, 40, 60)));
            dc.Clear();
            dc.SetTextForeground(*wxWHITE);
            dc.SetFont(wxFont(20, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
            dc.DrawText("Track Designer", 20, 20);
            dc.SetFont(wxFont(13, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));
            dc.DrawText("by Euro Model Trains", 20, 60);
        }
        if (cashierBmp.IsOk()) {
            int x = kImgW - cashierBmp.GetWidth() - 10;
            int y = kImgH - cashierBmp.GetHeight() - 10;
            dc.DrawBitmap(cashierBmp, x, y, false);
        }
    });

    sizer->Add(imgPanel, 0, wxEXPAND);

    // Credit text between image and button
    wxBoxSizer* creditSizer = new wxBoxSizer(wxVERTICAL);

    wxStaticText* byLine = new wxStaticText(this, wxID_ANY, "Track Designer by",
        wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
    wxFont bf2 = byLine->GetFont();
    bf2.SetPointSize(bf2.GetPointSize() + 1);
    byLine->SetFont(bf2);
    creditSizer->Add(byLine, 0, wxALIGN_CENTER | wxTOP, 6);

    wxHyperlinkCtrl* link = new wxHyperlinkCtrl(this, wxID_ANY,
        "Euro Model Trains", "https://www.euromodeltrains.com");
    wxFont lf = link->GetFont();
    lf.SetPointSize(lf.GetPointSize() + 1);
    lf.SetWeight(wxFONTWEIGHT_BOLD);
    link->SetFont(lf);
    creditSizer->Add(link, 0, wxALIGN_CENTER | wxBOTTOM, 4);

    sizer->Add(creditSizer, 0, wxEXPAND);

    // Button
    wxBoxSizer* btnSizer = new wxBoxSizer(wxHORIZONTAL);
    btnSizer->AddStretchSpacer();
    m_btnOK = new wxButton(this, wxID_OK, "  Done Staring  ");
    wxFont bf = m_btnOK->GetFont();
    bf.SetPointSize(bf.GetPointSize() + 1);
    bf.SetWeight(wxFONTWEIGHT_BOLD);
    m_btnOK->SetFont(bf);
    m_btnOK->SetMinSize(wxSize(280, 40));
    btnSizer->Add(m_btnOK, 0, wxALIGN_CENTER_VERTICAL | wxALL, 8);
    btnSizer->AddStretchSpacer();
    sizer->Add(btnSizer, 0, wxEXPAND);

    SetSizerAndFit(sizer);
    Centre();

    m_btnOK->Bind(wxEVT_BUTTON, &SplashDialog::OnOK, this);
    SetFocus();
}

void SplashDialog::OnOK(wxCommandEvent&) {
    EndModal(wxID_OK);
}

void SplashDialog::OnPaint(wxPaintEvent& e) {
    e.Skip();
}

void SplashDialog::OnKeyDown(wxKeyEvent& e) {
    if (e.GetKeyCode() == WXK_RETURN || e.GetKeyCode() == WXK_ESCAPE)
        EndModal(wxID_OK);
    else
        e.Skip();
}
