/***************************************************************************
 *   Copyright (C) 2013 by David S. Register                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
 **************************************************************************/

/**
 * \file
 *
 * Track Properties Dialog
 */

#ifndef TRACKPROPDLG_H_
#define TRACKPROPDLG_H_

#include <wx/artprov.h>
#include <wx/button.h>
#include <wx/checkbox.h>
#include <wx/choice.h>
#include <wx/clipbrd.h>
#include <wx/clipbrd.h>
#include <wx/colour.h>
#include <wx/colour.h>
#include <wx/dialog.h>
#include <wx/font.h>
#include <wx/font.h>
#include <wx/frame.h>
#include <wx/gdicmn.h>
#include <wx/gdicmn.h>
#include <wx/hyperlink.h>
#include <wx/icon.h>
#include <wx/image.h>
#include <wx/intl.h>
#include <wx/intl.h>
#include <wx/listctrl.h>
#include <wx/notebook.h>
#include <wx/panel.h>
#include <wx/radiobut.h>
#include <wx/scrolwin.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/string.h>
#include <wx/textctrl.h>
#include <wx/tglbtn.h>
#include <wx/xrc/xmlres.h>

#include "model/track.h"
#include "link_prop_dlg.h"

#ifdef __WXOSX__
#define DIALOG_PARENT wxFrame
#else
#define DIALOG_PARENT wxDialog
#endif

#define ID_RCLK_MENU_COPY_TEXT 7013

#define ID_TRK_MENU_ADD 7014
#define ID_TRK_MENU_EDIT 7015
#define ID_TRK_MENU_DELETE 7016

class TrackPropDlg;                    // forward
extern TrackPropDlg* pTrackPropDialog; /**< Global instance. */

class OCPNTrackListCtrl;  // forward

///////////////////////////////////////////////////////////////////////////////
/// Class TrackPropDlg
///////////////////////////////////////////////////////////////////////////////
class TrackPropDlg : public DIALOG_PARENT {
private:
  static bool instanceFlag;
  static TrackPropDlg* single;
  TrackPropDlg(wxWindow* parent, wxWindowID id, const wxString& title,
               const wxPoint& pos, const wxSize& size, long style);

  TrackPoint* m_pExtendPoint;
  Track* m_pExtendTrack;
  TrackPoint* m_pEntrackPoint;
  bool m_bStartNow;

  double m_planspeed;
  double m_avgspeed;

  int m_nSelected;  // index of point selected in Properties dialog row

  bool IsThisTrackExtendable();
  bool SaveChanges();

  HyperlinkList* m_pMyLinkList;
  void OnHyperLinkClick(wxHyperlinkEvent& event);
  wxHyperlinkCtrl* m_pEditedLink;
  void PopupMenuHandler(wxCommandEvent& event);
  void OnActivate(wxActivateEvent& event);

protected:
  wxNotebook* m_notebook1;
  wxScrolledWindow* m_panelBasic;
  wxStaticText* m_stName;
  wxTextCtrl* m_tName;
  wxStaticText* m_stFrom;
  wxTextCtrl* m_tFrom;
  wxStaticText* m_stTo;
  wxTextCtrl* m_tTo;
  wxCheckBox* m_cbShow;
  wxStaticText* m_stColor;
  wxChoice* m_cColor;
  wxStaticText* m_stStyle;
  wxChoice* m_cStyle;
  wxStaticText* m_stWidth;
  wxChoice* m_cWidth;
  wxStaticText* m_stTotDistance;
  wxTextCtrl* m_tTotDistance;
  wxStaticText* m_stAvgSpeed;
  wxTextCtrl* m_tAvgSpeed;
  wxStaticText* m_stTimeEnroute;
  wxTextCtrl* m_tTimeEnroute;
  wxStaticText* m_stShowTime;
  wxRadioButton* m_rbShowTimeUTC;
  /** Use system timezone to format date/time, i.e. timezone configured in the
   * operating system. */
  wxRadioButton* m_rbShowTimePC;
  /** Use Local Mean Time (LMT) at the location to format date/time. */
  wxRadioButton* m_rbShowTimeLocal;
  /**
   * Honor OpenCPN global setting to format date/time. This is the default value
   * to ensure consistent behavior with all date/time displays across the entire
   * application.
   */
  wxRadioButton* m_rbShowTimeGlobalSettings;
  OCPNTrackListCtrl* m_lcPoints;
  wxScrolledWindow* m_panelAdvanced;
  wxStaticText* m_stDescription;
  wxTextCtrl* m_tDescription;
  wxScrolledWindow* m_scrolledWindowLinks;
  wxHyperlinkCtrl* m_hyperlink1;
  wxMenu* m_menuLink;
  wxButton* m_buttonAddLink;
  wxToggleButton* m_toggleBtnEdit;
  wxStaticText* m_staticTextEditEnabled;
  wxStdDialogButtonSizer* m_sdbBtmBtnsSizer;
  wxButton* m_sdbBtmBtnsSizerOK;
  wxButton* m_sdbBtmBtnsSizerCancel;
  wxStaticBoxSizer* sbSizerLinks;
  wxBoxSizer* bSizerLinks;

  wxButton* m_sdbBtmBtnsSizerPrint;
  wxButton* m_sdbBtmBtnsSizerSplit;
  wxButton* m_sdbBtmBtnsSizerExtend;
  wxButton* m_sdbBtmBtnsSizerToRoute;
  wxButton* m_sdbBtmBtnsSizerExport;

  wxMenuItem* m_menuItemEdit;
  wxMenuItem* m_menuItemAdd;
  wxMenuItem* m_menuItemDelete;

  wxScrolledWindow* itemDialog1;
  bool m_bcompact;

  // Virtual event handlers, overide them in your derived class
  void OnCancelBtnClick(wxCommandEvent& event);
  void OnOKBtnClick(wxCommandEvent& event);
  void OnPrintBtnClick(wxCommandEvent& event);
  void OnSplitBtnClick(wxCommandEvent& event);
  void OnExtendBtnClick(wxCommandEvent& event);
  void OnToRouteBtnClick(wxCommandEvent& event);
  void OnExportBtnClick(wxCommandEvent& event);
  void OnTrackPropCopyTxtClick(wxCommandEvent& event);
  void OnTrackPropListClick(wxListEvent& event);
  void OnTrackPropRightClick(wxListEvent& event);
  void OnTrackPropMenuSelected(wxCommandEvent& event);
  void OnDeleteLink(wxCommandEvent& event);
  void OnEditLink(wxCommandEvent& event);
  void OnAddLink(wxCommandEvent& event);
  void OnEditLinkToggle(wxCommandEvent& event);
  void OnShowTimeTZ(wxCommandEvent& event);
  void CreateControls();
  void CreateControlsCompact();

public:
  static TrackPropDlg* getInstance(
      wxWindow* parent, wxWindowID id = wxID_ANY,
      const wxString& title = _("Track properties"),
      const wxPoint& pos = wxDefaultPosition,
      const wxSize& size = wxSize(680, 440),
      long style = FRAME_WITH_LINKS_STYLE);
  static bool getInstanceFlag() { return instanceFlag; }
  ~TrackPropDlg();

  void m_hyperlink1OnContextMenu(wxMouseEvent& event) {
    m_hyperlink1->PopupMenu(m_menuLink, event.GetPosition());
  }

  void SetTrackAndUpdate(Track* pt);
  bool UpdateProperties();
  void InitializeList();
  Track* GetTrack() { return m_pTrack; }

  void RecalculateSize();

  Track* m_pTrack;

  void m_hyperlinkContextMenu(wxMouseEvent& event);
};

class OCPNTrackListCtrl : public wxListCtrl {
protected:
  /** Return the longitude at the start point of the track. */
  double getStartPointLongitude() const;

public:
  OCPNTrackListCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos,
                    const wxSize& size, long style);
  ~OCPNTrackListCtrl();

  wxString OnGetItemText(long item, long column) const;
  int OnGetItemColumnImage(long item, long column) const;

  Track* m_pTrack;
  /**
   * The timezone to use when formatting date/time.
   * Possible values are:
   * - 0: UTC
   * - 1: Timezone configured in operating system
   * - 2: Mean solar time at the location, based on the average time it takes
   *      for the sun to cross the meridian (appear at its highest point in the
   * sky) at that specific location.
   * - 3: Honor OpenCPN global setting for timezone display
   */
  int m_tz_selection;
  int m_LMT_Offset;
};

#endif  // TRACKPROPDLG_H_
