/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/


/*-------------------------- Pro/Toolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProUtil.h>
#include <ProMessage.h>
#include <ProNavigator.h>
#include <ProArray.h>


/*-------------------------- Application includes ---------------------------*/
#include <TestError.h>
#include <UtilFiles.h>
#include <UtilString.h>
#include <PTApplsUnicodeUtils.h>

#define MSGFIL L##"msg_ugmenu.txt"


ProError UgAddNewPane();
ProError UgChangeNavPaneUrl();
ProError UgChangeNavPaneIcon();
ProError UgNavPaneInfoGet();

/*================================================================*\
FUNCTION  : UgAddNewPane()
PURPOSE   : Adds new Navigator Pane
\*================================================================*/

ProError status;
       
  
ProError UgAddNewPane()
{
  wchar_t w_pane_name[PRO_LINE_SIZE];
  ProCharName pane_name;
  wchar_t url1[PRO_LINE_SIZE];
  
  status = ProMessageDisplay (MSGFIL, "Please enter name for the new pane");
  status = ProMessageStringRead(PRO_LINE_SIZE, w_pane_name);
 
  if(status != PRO_TK_NO_ERROR)
    return (status);
    
  ProWstringToString (pane_name, w_pane_name);

  ProMessageDisplay (MSGFIL, "Please enter URL for the pane");
  status = ProMessageStringRead(PRO_LINE_SIZE, url1);

  if(status != PRO_TK_NO_ERROR)
    return (status);
         
  status = ProNavigatorpaneBrowserAdd(pane_name, NULL, url1);
  ERROR_CHECK("ProNavigatorpaneBrowserAdd","ProNavigatorpaneBrowserAdd",status);
  if(status == PRO_TK_NO_ERROR)
    ProMessageDisplay (MSGFIL, "Added new pane");
  else
    ProMessageDisplay (MSGFIL, "Error in new pane creation");   

  return (status);
}

/*================================================================*\
FUNCTION  : UgAddNewDialogPane()
PURPOSE   : Creates the Navigator Pane with Dialog per resource file
\*================================================================*/
     
  
ProError UgAddNewDialogPane()
{
  wchar_t w_pane_name[PRO_LINE_SIZE];
  ProCharName pane_name;
  ProCharName resource_name;
    
  status = ProMessageDisplay (MSGFIL, "Please enter name for the new pane");
  status = ProMessageStringRead(PRO_LINE_SIZE, w_pane_name);
 
  if(status != PRO_TK_NO_ERROR)
    return (status);
    
  ProWstringToString (pane_name, w_pane_name);

  strcpy(resource_name,"ug_navigatorpane.res");

  status = ProNavigatorpanePHolderAdd (pane_name, resource_name, NULL);
  ERROR_CHECK("UgAddNewDialogPane","ProNavigatorpanePHolderAdd",status);
 
  if(status == PRO_TK_NO_ERROR)
    ProMessageDisplay (MSGFIL, "Added new pane");
  else
    ProMessageDisplay (MSGFIL, "Error in new pane creation");  

  return (status);
}

/*================================================================*\
FUNCTION  : UgChangeNavPaneUrl()
PURPOSE   : Changes Navigator Panes URL
\*================================================================*/

ProError UgChangeNavPaneUrl()
{
  wchar_t w_pane_name[PRO_LINE_SIZE];
  ProCharName pane_name;
  wchar_t url1[PRO_LINE_SIZE];
  
  status = ProMessageDisplay (MSGFIL, "Please enter the pane name for changing URL");
  status = ProMessageStringRead(PRO_LINE_SIZE, w_pane_name);
  
  if(status != PRO_TK_NO_ERROR)
    return (status);
    
  ProWstringToString (pane_name, w_pane_name);
  
  status = ProMessageDisplay (MSGFIL, "Please enter the new URL");
  status = ProMessageStringRead(PRO_LINE_SIZE, url1);

  if(status != PRO_TK_NO_ERROR)
    return (status);
      
  status = ProNavigatorpaneBrowserURLSet(pane_name, url1);
  ERROR_CHECK("ProNavigatorpaneBrowserAdd","ProNavigatorpaneBrowserURLSet",status);
                 
  if (status != PRO_TK_NO_ERROR)
    ProMessageDisplay (MSGFIL, "Pane with entered name does not exist");
    
  else
    ProMessageDisplay (MSGFIL, "Pane URL Updated");      

  return(status);
}

/*================================================================*\
FUNCTION  : UgChangeNavPaneIcon()
PURPOSE   : Changes Navigator Panes Icon
\*================================================================*/

ProError UgChangeNavPaneIcon()
{
  wchar_t w_pane_name[PRO_LINE_SIZE];
  ProCharName pane_name;
  wchar_t w_icon_file[PRO_LINE_SIZE];
  ProCharName icon_file_name;
  ProLine buff;
  ProPath *path_arr, sel_path, def_path;
  ProName *path_lab_arr;
       
  status = ProMessageDisplay (MSGFIL, "Please enter the pane name for changing icon");
  status = ProMessageStringRead(PRO_LINE_SIZE, w_pane_name);

  if(status != PRO_TK_NO_ERROR)
    return (status);
    
  ProWstringToString (pane_name, w_pane_name);
  ProMessageDisplay(MSGFIL, "Choose new Icon file");

  /*-----------------------------------------------------------------*\
	  Prompt the user to select the file.
  \*-----------------------------------------------------------------*/
  ProStringToWstring(buff, "*.jpg,*.gif");
  ProStringToWstring(def_path, ".");

  ProArrayAlloc(0, sizeof(ProPath), 1, (ProArray*)&path_arr);
  ProArrayAlloc(0, sizeof(ProPath), 1, (ProArray*)&path_lab_arr);
  status = ProFileMdlnameOpen(NULL, buff, path_arr, path_lab_arr, def_path, NULL,sel_path);
 
  if (status != PRO_TK_NO_ERROR)
    ProMessageDisplay (MSGFIL, "No file selected");
    
  else
  {
    ProWstringToString (icon_file_name, sel_path);
    status = ProNavigatorpaneBrowsericonSet(pane_name, icon_file_name);
    ERROR_CHECK("UgChangeNavPaneIcon","ProNavigatorpaneBrowsericonSet",status);

    if (status != PRO_TK_NO_ERROR)
      ProMessageDisplay (MSGFIL, "Pane with entered name does not exist");

    else
      ProMessageDisplay (MSGFIL, "Pane icon Updated");
  }
                 

      
  return (status);
}

/*================================================================*\
FUNCTION  : UgNavPaneInfoGet()
PURPOSE   : Gives Navigator Pane information
\*================================================================*/

ProError UgNavPaneInfoGet()
{
  wchar_t w_pane_name[PRO_LINE_SIZE];
  ProCharName pane_name;
  ProCharName icon_file_name;
  wchar_t url1[PRO_LINE_SIZE];
  char* comp_name;
  char* layout_name;
  char* layout_name2;
  int win_id;
  char * device_name;
  char *nominal_name = "LocalName";
  FILE *fp;
  char name[PRO_NAME_SIZE];
  wchar_t wname[PRO_NAME_SIZE];
   
  fp = PTApplsUnicodeFopen("info.txt","w");
  status = ProMessageDisplay (MSGFIL, "Please enter the pane name");
  status = ProMessageStringRead(PRO_LINE_SIZE, w_pane_name);

  if(status != PRO_TK_NO_ERROR)
    return (status);
    
  ProWstringToString (pane_name, w_pane_name);
  
  status = ProWindowCurrentGet(&win_id);
  ERROR_CHECK("UgNavPaneInfoGet","ProWindowCurrentGet",status);
  if( status == PRO_TK_NO_ERROR)
  {
    status = ProNavigatorpanePHolderDevicenameGet (win_id, &device_name);
    ERROR_CHECK("UgNavPaneInfoGet","ProNavigatorpanePHolderDevicenameGet",status);

    if (status != PRO_TK_NO_ERROR)
      return (status);
    else
    {
      ProTKFprintf(fp, "Device name is: %s \n", device_name);
      ProStringFree(device_name);
    }
  }
  else
    return (status);
         
  status = ProNavigatorpanePHolderLayoutGet (pane_name, &layout_name);
  ERROR_CHECK("UgNavPaneInfoGet","ProNavigatorpanePHolderLayoutGet",status);
  if (status != PRO_TK_NO_ERROR)
  {
    ProMessageDisplay (MSGFIL, "Pane with entered name does not exist");
    return (status);
  }
  else
  {  
    ProTKFprintf(fp, "layout name is: %s \n", layout_name);
    ProStringFree(layout_name);
  }

  status = ProNavigatorpanePHolderComponentnameGet (pane_name, nominal_name,&comp_name);
  ERROR_CHECK("UgNavPaneInfoGet","ProNavigatorpanePHolderComponentnameGet",status);
  if (status != PRO_TK_NO_ERROR)
  {
    ProMessageDisplay (MSGFIL, "Pane with entered name does not exist");
    return (status);
  }
  else
  {
    ProTKFprintf(fp, "Component name is: %s \n", comp_name);
    ProStringFree(comp_name);
  }

  fclose(fp);
  ProStringToWstring(wname, "info.txt");
  ProInfoWindowDisplay(wname, NULL, NULL);
    
  return (status);
}