#include "ftpro.h"

BEGIN_EVENT_TABLE(HarborDialog,wxDialog)
  EVT_MENU(ID_REMOTE_NEW_DIR_RC,HarborDialog::OnRemoteNewDir)
  EVT_MENU(ID_REMOVE_DIR_RC,HarborDialog::OnRemoteRemove)
  EVT_MENU(ID_REMOTE_GET_RC,HarborDialog::OnRemoteGetMany)
  EVT_MENU(ID_REMOTE_PWD_RC,HarborDialog::OnRemotePWDRC)
  EVT_MENU(ID_REMOTE_REFRESH_RC,HarborDialog::OnRemoteRefresh)
  EVT_MENU(ID_REMOTE_OWNER_RC,HarborDialog::OnRemoteOwner)
  EVT_MENU(ID_REMOTE_LIST_RC,HarborDialog::OnRemoteList)
  EVT_MENU(ID_REMOTE_RO_RC, HarborDialog::OnRemoteRegularOpen)
  EVT_MENU(ID_REMOTE_PO_RC, HarborDialog::OnRemoteProgrammersOpen)
  EVT_MENU(ID_LOCAL_NEW_DIRECTORY_RC,HarborDialog::OnLocalNewDir)//End Remote Right click
  EVT_MENU(ID_LOCAL_NEW_FILE_RC, HarborDialog::OnLocalNewFile)
  EVT_MENU(ID_LOCAL_REMOVE_RC, HarborDialog::OnLocalRemove)
  EVT_MENU(ID_LOCAL_PUT_RC, HarborDialog::OnLocalPut)
  EVT_MENU(ID_LOCAL_OPENR_RC, HarborDialog::OnLocalRegularOpen)
  EVT_MENU(ID_LOCAL_REFRESH_RC, HarborDialog::OnLocalRefresh)
  EVT_MENU(ID_LOCAL_OPENP_RC,HarborDialog::OnLocalProgrammerOpen) // Local Right Click

  EVT_BUTTON( ID_HARBOR_QUIT, HarborDialog::OnQuitAll )
  EVT_BUTTON( ID_HARBOR_HELP, HarborDialog::OnHelp )
  EVT_BUTTON( ID_LOCAL_HOME, HarborDialog::OnLocalHome )
  EVT_BUTTON( ID_LOCAL_CDUP, HarborDialog::OnLocalCdup )
  EVT_BUTTON( ID_REMOTE_HOME, HarborDialog::OnRemoteHome )
  EVT_BUTTON( ID_REMOTE_CDUP, HarborDialog::OnRemoteCdup )
  EVT_LIST_ITEM_ACTIVATED( ID_LOCAL_LIST, HarborDialog::OnLocalActivated )
  EVT_RIGHT_DOWN( HarborDialog::OnLocalRightClick )
  EVT_LIST_BEGIN_DRAG( ID_LOCAL_LIST, HarborDialog::OnLocalBeginDrag )
  EVT_COMBOBOX( ID_LOCAL_PWD_COMBO, HarborDialog::OnLocalPWD )
  EVT_TEXT(ID_LOCAL_PWD_COMBO,HarborDialog::OnLocalPWDTextChange)
  EVT_LIST_ITEM_ACTIVATED( ID_REMOTE_LIST, HarborDialog::OnRemoteActivated )
  EVT_LIST_ITEM_RIGHT_CLICK( ID_REMOTE_LIST, HarborDialog::OnRemoteRightClicked )
  EVT_LIST_BEGIN_DRAG( ID_REMOTE_LIST, HarborDialog::OnRemoteBeginDrag )
  EVT_BUTTON( ID_TRANSFER_ABORT, HarborDialog::OnTransferAbort )
  EVT_BUTTON( ID_TRANSFER_RESUME, HarborDialog::OnTransferResume )
  EVT_BUTTON( ID_SETUP_ABOUT, MyFrame::OnAbout)
  EVT_BUTTON( ID_SETUP_HELP, HarborDialog::OnHelp)
  EVT_RADIOBOX( ID_SETUP_STYLE_RB, HarborDialog::OnSetupStyle )
  EVT_CLOSE(HarborDialog::OnQuitAll)
END_EVENT_TABLE()


HarborDialog::HarborDialog( wxWindow *parent, wxWindowID id, const wxString &title,
    const wxPoint &position, const wxSize& size, long style ) :
    wxDialog( parent, id, title, position, size, style )
{
  int style;
  if (!(wxDirExists("C:\\Ftpro")))
    {
      if (!(wxMkdir("C:\\Ftpro")))
        {
          wxMessageDialog dialog(this, "Hmm, looks unrecoverable\nI cant write to a directory i need to write too\nfix it and I might work.....maybe","Error",wxOK | wxICON_ERROR);
          dialog.ShowModal();
          exit(0);
        }
    }

  if (!(wxFileExists("C:\\Ftpro\\.ftpro_style")))
    {
      wxString message = "Welcome first time user!!\nThis is the alpha release of Ftpro.  Make liberal use of the right mouse button, and see RFC959 for programming details.  Enjoy!!";
      wxMessageDialog dialog(this, message,"Welcome!", wxOK |wxICON_INFORMATION);
      dialog.ShowModal();
      ofstream styletype("C:\\Ftpro\\.ftpro_style");
      styletype << 0;
      styletype.close();
      ofstream start("C:\\Ftpro\\.startup");
      start << 1;
      start.close();
      ofstream peditor("C:\\Ftpro\\.peditor");
      peditor << "C:\\Program Files\\Accessories\\wordpad.exe%";
      peditor.close();
      ofstream reditor("C:\\Ftpro\\.reditor");
      reditor << "C:\\Program Files\\Accessories\\wordpad.exe%";
      reditor.close();
      ofstream home("C:\\Ftpro\\.home");
      home << "C:\\FTPro%";
      home.close();
      ofstream qc("C:\\Ftpro\\.qc");
      qc << " \n";
      qc.close();
      SetHome("C:\\Ftpro");
      SetRegularEditor("C:\\Program Files\\Accessories\\wordpad.exe");
      SetProgrammingEditor("C:\\Program Files\\Accessories\\wordpad.exe");
      style = 0;

    }
  else
    {
      //need a home_char cause data overflows, and you cant set it because, well you know why
      char data[200],home_char[200];
      wxString temp1,temp2;
      ifstream styletype("C:\\Ftpro\\.ftpro_style");
      styletype.read(data,10);
      style = (data[0] - '0');
      ifstream peditor("C:\\Ftpro\\.peditor");
      peditor.read(data,200);
      peditor.close();
      temp1 = data;
      temp2 = temp1.BeforeLast('%');
      SetProgrammingEditor(temp2);
      ifstream reditor("C:\\Ftpro\\.reditor");
      reditor.read(data,200);
      reditor.close();
      temp1 = data;
      temp2 = temp1.BeforeLast('%');
      SetRegularEditor(temp2);
      ifstream home("C:\\Ftpro\\.home");
      home.read(home_char,100);
      home.close();
      temp1 = home_char;
      temp2 = temp1.BeforeLast('%');
      SetHome(temp2);
    }

  
  if (style == 0 || style == 1)
    SetStyle(1);
  else
    SetStyle(0);

    wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );

    wxButton *item2 = new wxButton( this, ID_HARBOR_QUIT, "Quit", wxDefaultPosition, wxSize(110,-1), 0 );
    item2->SetFont( wxFont( 10, wxMODERN, wxNORMAL, wxNORMAL ) );
    item2->SetToolTip( "Quit FTPro" );
    item1->Add( item2, 0, wxALIGN_CENTRE|wxALL , 5 );

    item1->Add( 200, 10, 1, wxALIGN_CENTRE|wxALL  , 0 );

    item1->Add( 300, 20, 1, wxALIGN_CENTRE|wxALL  , 0 );

    wxButton *item3 = new wxButton( this, ID_HARBOR_HELP, "Help", wxDefaultPosition, wxSize(110,-1), wxNO_BORDER );
    item3->SetDefault();
    item3->SetFont( wxFont( 10, wxMODERN, wxNORMAL, wxNORMAL ) );

    item1->Add( item3, 0, wxALIGN_CENTRE|wxALL , 10 );

    item0->Add( item1, 0, wxALIGN_CENTER_VERTICAL|wxALL | wxEXPAND, 0 );

    wxNotebook *item5 = new wxNotebook( this, ID_HARBOR_NOTEBOOK, wxDefaultPosition, wxSize(200,160), wxNB_BOTTOM|wxNB_FIXEDWIDTH );
    wxNotebookSizer *item4 = new wxNotebookSizer( item5 );

    wxPanel *item6 = new wxPanel( item5,-1);
      //    wxPanel *item6 = new wxPanel( item5, -1 );
    connect_page( item6, FALSE,TRUE,&local_pwd_cb, &remote_pwd_cb, style);
    item5->AddPage( item6, "Main" );

    wxPanel *item7 = new wxPanel( item5, -1 );
    transfer_page( item7, FALSE );
    item5->AddPage( item7, "Transfers" );

    wxPanel *item8 = new wxPanel( item5, -1 );
    status_page( item8, FALSE,TRUE,&status_list );
    item5->AddPage( item8, "Status" );

    wxPanel *item9 = new wxPanel( item5, -1 );
    setup_page( item9, FALSE,TRUE,&style_radio, style);
    item5->AddPage( item9, "Setup" );

    item0->Add( item4, 6, wxALIGN_CENTRE|wxALL| wxEXPAND , 0 );

    HarborDialog::harbor_text = new wxTextCtrl( this, ID_HARBOR_TEXTCTRL, "Not Connected\n\n", wxDefaultPosition, wxSize(740,110), wxTE_MULTILINE );
    HarborDialog::harbor_text->SetFont( wxFont( 12, wxMODERN, wxNORMAL, wxNORMAL ) );
    HarborDialog::harbor_text->SetToolTip( "FTP Commands, see what goes on backstage!" );
    item0->Add( HarborDialog::harbor_text, 2, wxALIGN_CENTRE|wxALL | wxEXPAND, 5 );

    if (1)
    {
        this->SetAutoLayout( TRUE );
        this->SetSizer( item0 );
        if (1)
        {
            item0->Fit( this );
            item0->SetSizeHints( this );
        }
    }

}

// WDR: handler implementations for HarborDialog
void HarborDialog::OnTransferAbort(wxCommandEvent &event)
{}

void HarborDialog::OnTransferResume(wxCommandEvent &event)
{}


void HarborDialog::OnSetupStyle( wxCommandEvent &event )
{
  int style = style_radio->GetSelection();
  ofstream out("C:\\FTPro\\.ftpro_style");
  out << style;
  out.close();
  wxMessageDialog dialog(this, "This style change will not take affect until you restart the application\nyou want me too restart (This will end the current session)?"," Style Change",wxOK | wxCANCEL);
  if (dialog.ShowModal() == wxID_OK)
    {
      wxExecute("C:\\Program Files\\Ftpro\\ftpro.exe");
      exit(0);
    }
  else
    dialog.EndModal(1);
  
}

void HarborDialog::OnHelp( wxCommandEvent &event )
{
  HelpDialog h(this,-1,"Help");
  h.ShowModal();
}

void HarborDialog::OnQuit( wxCommandEvent &event )
{
    
}

int HarborDialog::GetSelected(wxListCtrl* list)
{
  long item, sel;
  item = -1;
  sel = 0;
  for (;;)
    {
      item = list->GetNextItem(item,
                               wxLIST_NEXT_ALL,
                               wxLIST_STATE_SELECTED);
      if ( item == -1 )
        break;
      
      sel = item;
    }
  
  return sel;
}

int HarborDialog::GetSelected(wxListCtrl* list, vector<int> &v)
{
  long item, sel, i;
  item = -1;
  i = sel = 0;
  
  for (;;)
    {
      item = list->GetNextItem(item,
                               wxLIST_NEXT_ALL,
                               wxLIST_STATE_SELECTED);
      if ( item == -1 )
        break;
      sel = item;
      v.push_back(sel);
    }
  // some kinda bug i guess
  v[v.size() -1] = v.back() - 2;
  return sel;

}