/////////////////////////////////////////////////////////////////////////////
// Name:        TitlePropDlg.cpp
// Purpose:     DVD title properties dialog
// Author:      Alex Thuering
// Created:     31.01.2004
// RCS-ID:      $Id: TitlePropDlg.cpp,v 1.22 2008/02/10 16:49:30 ntalex Exp $
// Copyright:   (c) Alex Thuering
// Licence:     GPL
/////////////////////////////////////////////////////////////////////////////

#include "TitlePropDlg.h"
#include "TitlesetManager.h"
#include "AVPropDlg.h"

enum
{
  AV_BT_ID = 7850
};

BEGIN_EVENT_TABLE(TitlePropDlg, wxPropDlg)
  EVT_BUTTON(AV_BT_ID, TitlePropDlg::OnAV)
END_EVENT_TABLE()

TitlePropDlg::TitlePropDlg(wxWindow* parent, DVD* dvd,
 Audio* audio, Video* video, Pgc* pgc, Vob* vob):
  wxPropDlg(parent), m_dvd(dvd), m_audio(audio), m_video(video), m_pgc(pgc), m_vob(vob)
{
  Create();
  SetSize(360,-1);
}

void TitlePropDlg::CreatePropPanel(wxSizer* sizer)
{
  if (m_vob->GetSlideshow())
  {
    Slideshow* slideshow = m_vob->GetSlideshow();
    wxSizer* grpSizer = BeginGroup(sizer, _("Slideshow"));
    wxFlexGridSizer* grid = new wxFlexGridSizer(2, 2, 4, 16);
    grid->AddGrowableCol(1);
    wxArrayString formats = DVD::GetVideoFormatStrings();
    AddComboProp(grid, _("Format:"), formats[slideshow->GetVideoFormat()],
      formats, wxCB_READONLY);
    AddSpinProp(grid, _("Duration (sec):"), slideshow->GetDuration(), 1, 1000, 100);
    grpSizer->Add(grid, 0, wxEXPAND|wxALL, 6);
  }
  else
  {
    wxSizer* grpSizer = BeginGroup(sizer, _("Video object"));
    wxFlexGridSizer* grid = new wxFlexGridSizer(2, 4, 16);
    grid->AddGrowableCol(1);
    if (m_vob->GetFilename().length() || m_vob->GetVideoFilename().length())
    {
        if (m_vob->GetFilename().length())
          AddStaticTextProp(grid, _("File name:"), m_vob->GetFilename());
        else
          AddStaticTextProp(grid, _("Video:"), m_vob->GetVideoFilename());
        if (m_vob->GetDuration()>0)
        {
          int secs = (int) m_vob->GetDuration();
          int mins = secs / 60;
          secs %= 60;
          int hours = mins / 60;
          mins %= 60;
          AddStaticTextProp(grid, _("Duration:"),
            wxString::Format(wxT("%02d:%02d:%02d"), hours, mins, secs));
        }
        else
          AddStaticTextProp(grid, _("Duration:"), wxT("N/A"));
        wxSize size = m_vob->GetVideoSize();
        wxString vs = wxT("N/A");
        if (size.IsFullySpecified())
        {
          vs = wxString::Format(wxT("%dx%d"), size.GetWidth(), size.GetHeight());
          if (size.GetWidth() == 720 && size.GetHeight() == 576)
              vs += wxT(" (PAL)");
          else if (size.GetWidth() == 720 && size.GetHeight() == 480)
              vs += wxT(" (NTSC)");
        }
        AddStaticTextProp(grid, _("Video size:"), vs);
    }
    for (unsigned int i=0; i<m_vob->GetAudioFilenames().Count(); i++)
    {
      grid->Add(1,1); grid->Add(1,1);
      AddStaticTextProp(grid, _("Audio:"), m_vob->GetAudioFilenames()[i]);
    }
    for (unsigned int i=0; i<m_vob->GetSubtitles().Count(); i++)
    {
      grid->Add(1,1); grid->Add(1,1);
      AddStaticTextProp(grid, _("Subtitle:"), m_vob->GetSubtitles()[i]->GetFilename());
    }
    grid->Add(2,2); grid->Add(2,2);
    AddTextProp(grid, _("Chapters:"), m_vob->GetChapters());
    AddSpinProp(grid, _("Pause:"), m_vob->GetPause(), -1, 254, 100);
    grpSizer->Add(grid, 0, wxEXPAND|wxALL, 6);
  }
  sizer->Add(6,6);
  
  wxSizer* grpSizer = BeginGroup(sizer, _("Title"));
  wxFlexGridSizer* grid = new wxFlexGridSizer(2, 2, 4, 16);
  grid->AddGrowableCol(1);
  AddFileProp(grid, _("Palette:"), m_pgc->GetPalette(), wxFD_OPEN, wxT("..."), wxT("*.rgb;*.yuv"));
  wxArrayString commands;
  for (int pgci=0; pgci<(int)m_dvd->GetVmgm().Count(); pgci++)
	commands.Add(wxString::Format(_T("call vmgm menu %d;"),pgci+1));
  for (int tsi=0; tsi<(int)m_dvd->GetTitlesets().Count(); tsi++)
  {
	Titleset* ts = m_dvd->GetTitlesets()[tsi];
	if (ts->GetMenus().Count()>0)
	  commands.Add(_T("call menu;"));
	if (m_dvd->IsJumppadsEnabled())
	{
	  for (int pgci=1; pgci<(int)ts->GetMenus().Count(); pgci++)
		commands.Add(wxString::Format(_T("call menu %d;"),pgci+1));
	}
	for (int pgci=0; pgci<(int)ts->GetTitles().Count(); pgci++)
	  commands.Add(wxString::Format(_T("jump title %d;"),pgci+1));
  }
  AddComboProp(grid, _("Pre commands:"), m_pgc->GetPreCommands(), commands);
  AddComboProp(grid, _("Post commands:"), m_pgc->GetPostCommands(), commands);
  grpSizer->Add(grid, 0, wxEXPAND|wxALL, 6);
}

bool TitlePropDlg::SetValues()
{
  int n = 0;
  if (m_vob->GetSlideshow())
  {
    Slideshow* slideshow = m_vob->GetSlideshow();
    slideshow->SetVideoFormat((VideoFormat) GetInt(n++));
    slideshow->SetDuration(GetInt(n++));
  }
  else
  {
    if (m_vob->GetFilename().length() ||
        m_vob->GetVideoFilename().length())
      n += 3;
    n += m_vob->GetAudioFilenames().Count();
    n += m_vob->GetSubtitles().Count();
    m_vob->SetChapters(GetString(n++));
    m_vob->SetPause(GetInt(n++));
  }
  m_pgc->SetPalette(GetString(n++));
  m_pgc->SetPreCommands(GetString(n++));
  m_pgc->SetPostCommands(GetString(n++));
  return true;
}

void TitlePropDlg::CreateButtonPane(wxSizer* sizer, bool resetButton)
{
  wxBoxSizer* buttonPane = new wxBoxSizer(wxHORIZONTAL);
  wxButton* avBt = new wxButton(this, AV_BT_ID, _("Audio/Video..."));
  buttonPane->Add(avBt, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL);
  buttonPane->Add(10, 10, 1, wxEXPAND);
  wxButton* okBt = new wxButton(this, wxID_OK, _("OK"));
  okBt->SetDefault();
#if defined(__WXGTK__) && wxCHECK_VERSION(2,5,2) && !wxCHECK_VERSION(2,5,4)
  okBt->SetSizeHints(-1, okBt->GetSize().GetY()+8);
#endif
  buttonPane->Add(okBt, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL);
  buttonPane->Add(8, 0);
  wxButton* cancelBt = new wxButton(this, wxID_CANCEL, _("Cancel"));
  buttonPane->Add(cancelBt, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL);
  sizer->Add(buttonPane, 0, wxEXPAND|wxLEFT|wxRIGHT|wxBOTTOM, 10);
}

void TitlePropDlg::OnAV(wxCommandEvent& event)
{
  AVPropDlg propDlg(this, m_audio, m_video, false);
  if (propDlg.ShowModal() == wxID_OK)
  {
	wxCommandEvent evt(EVT_COMMAND_DVD_CHANGED, this->GetId());
	GetEventHandler()->ProcessEvent(evt);
  }
}
