////////////////////////////////////////////////////////////////////////////////
//
// Converter -   -  
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"

#include "LibConverter.h"
#include "Resource.h"
#include "MyConverter.h"
#include "KompasConverter.h"
#include "LibConverterParam.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern unsigned int WINAPI LIBRARYID();

//    
BOOL ShowPropDlg( CWnd * pParent = NULL );


//   
LibConverter * converter = NULL;


//-------------------------------------------------------------------------------
//   
// ---
LibConverter & GetLibConverter()
{
  if ( !converter )
    converter = new LibConverter();
  return *converter; 
}


//-------------------------------------------------------------------------------
//   (     OnProcessDetach)
// ---
void ConverterDestroy() 
{
  delete converter;
  converter = NULL;
}


//-------------------------------------------------------------------------------
// 
// ---
LibConverter::LibConverter()
 : lineStyle        ( TRUE ),
   m_pConverter     ( NULL ),
   m_pConverterParam( NULL )
{
}


//-------------------------------------------------------------------------------
// 
// ---
LibConverter::~LibConverter()
{
}


//-------------------------------------------------------------------------------
//     
// ---
KompasConverter * LibConverter::GetKompasConverter()
{
  if ( !m_pConverter )
    m_pConverter = new KompasConverter();  //    AddRef 
  else
    m_pConverter->InternalAddRef();        //   AddRef   
  return m_pConverter;
}


//-----------------------------------------------------------------------------
//    
// ..       -    
// ---
LibConverterParam * LibConverter::GetConvertParam( long command )
{
  if ( !m_pConverterParam )
    m_pConverterParam = new LibConverterParam();  //    AddRef 
  else
    m_pConverterParam->InternalAddRef();          //   AddRef   
  return m_pConverterParam;
}

//-----------------------------------------------------------------------------
//    
// ---
BOOL LibConverter::VisualEditConvertParam( OLE_HANDLE parentHwnd, long command )
{
  CWnd * pCWnd = CWnd::FromHandlePermanent( (HWND)parentHwnd );

  return ShowPropDlg( pCWnd );
}


//-------------------------------------------------------------------------------
//     :
// inputFile -   
// outFile -    
// command -     (MyConvEnum)
// showParam - TRUE -   , FALSE - 
//  : 1 -  , 0 -  
// ---
long LibConverter::Convert( LPCTSTR inputFile, LPCTSTR outFile, long command, BOOL showParam )
{
  long res = 0;
  //  TRUE -      
  if ( showParam )
    VisualEditConvertParam( (OLE_HANDLE)(HWND)::GetHWindow(), command );
  
  //  
  CString mess;
  
  //       
  switch ( command )
  {
    case libConv_Exp_m3d :
    {  
      mess.Format( _T(" libConv_Exp_m3d\n %s   %s\n  (*.mym3d) %s"), 
                    inputFile && _tcslen(inputFile) ? _T("") : _T(" "),
                    inputFile && _tcslen(inputFile) ? inputFile : _T(""),
                    outFile
                 );
      if ( !inputFile )
        ::SaveDocumentExT(::ksGetCurrentDocument(0), (LPTSTR)outFile, 0 );
      res = 1;
      break;
    }
    case libConv_Exp_a3d :
    { 
      mess.Format( _T(" libConv_Exp_a3d\n %s   %s\n  (*.mya3d) %s"), 
                    inputFile && _tcslen(inputFile) ? _T("") : _T(" "),
                    inputFile && _tcslen(inputFile) ? inputFile : _T(""),
                    outFile
                 );
      res = 1;
      break;
    }
    case libConv_Exp_frw :
    {
      mess.Format( _T(" libConv_Exp_frw\n %s  %s\n  (*.myfrw) %s"), 
                    inputFile && _tcslen(inputFile) ? _T("") : _T(" "),
                    inputFile && _tcslen(inputFile) ? inputFile : _T(""),
                    outFile
                 );
      res = 1;
      break;
    }
    case libConv_Exp_cdw :
    { 
      mess.Format( _T(" libConv_Exp_cdw\n %s   %s\n  (*.mycdw) %s"), 
                    inputFile && _tcslen(inputFile) ? _T("") : _T(" "),
                    inputFile && _tcslen(inputFile) ? inputFile : _T(""),
                    outFile
                 );
      res = 1;
      break;
    }
    case libConv_Exp_spw :
    {  
      mess.Format( _T(" libConv_Exp_spw\n %s   %s\n  (*.myspw) %s"), 
                    inputFile && _tcslen(inputFile) ? _T("") : _T(" "),
                    inputFile && _tcslen(inputFile) ? inputFile : _T(""),
                    outFile
                 );
      res = 1;
      break;
    }
    case libConv_Exp_kdw :
    {
      mess.Format( _T(" libConv_Exp_kdw\n    %s\n  (*.mykdw) %s"), 
                    inputFile && _tcslen(inputFile) ? _T("") : _T(" "),
                    outFile
                 );
      res = 1;
      break;
    }

   
    case libConv_Imp_m3d :
    { 
      mess.Format( _T(" libConv_Imp_m3d\n  (*.mym3d)\n %s ->\n%s  "), 
                    inputFile,
                    outFile && _tcslen(outFile) ? outFile : _T("  .")
                 );
      ::OpenDocument( (char *)inputFile, 0/*regim*/);
      res = 1;
      break;
    }
    case libConv_Imp_a3d :
    {
      mess.Format( _T(" libConv_Imp_a3d\n  (*.mya3d)\n %s ->\n%s  "), 
                    inputFile,
                    outFile && _tcslen(outFile) ? outFile : _T("  .")
                 );
      res = 1;
      break;
    }
    case libConv_Imp_frw :
    {
      mess.Format( _T(" libConv_Imp_frw\n  (*.myfrw)\n %s ->\n%s  "), 
                    inputFile,
                    outFile && _tcslen(outFile) ? outFile : _T("  .")
                 );
      res = 1;
      break;
    }
    case libConv_Imp_cdw :
    {
      mess.Format( _T(" libConv_Imp_cdw\n  (*.mycdw)\n %s ->\n%s  "), 
                    inputFile,
                    outFile && _tcslen(outFile) ? outFile : _T("  .")
                 );
      res = 1;
      break;
    }
    case libConv_Imp_spw :
    {
      mess.Format( _T(" libConv_Imp_spw\n  (*.myspw)\n %s ->\n%s  "), 
                    inputFile,
                    outFile && _tcslen(outFile) ? outFile : _T("  .")
                 );
      res = 1;
      break;
    }
    case libConv_Imp_kdw :
    {
      mess.Format( _T(" libConv_Imp_kdw\n  (*.mykdw)\n %s ->\n%s  "), 
                    inputFile,
                    outFile && _tcslen(outFile) ? outFile : _T("  ")
                 );
      res = 1;
      break;
    }
  }
  
  //    
  CString buf;
  buf.Format( _T("\n     - %s"), lineStyle ? _T(".")
                                                                           : _T("  ."));
  mess += buf;
  //  
  LibMessage( mess );
  return res;
}



//-------------------------------------------------------------------------------
//    -       rc2
// ---
BOOL LibConverter::GetCanUnLoadLibrary()
{
  return m_pConverterParam ? FALSE : TRUE;
}

//-------------------------------------------------------------------------------
//    -       rc2
// ---
void LibConverter::ExecuteCommand( unsigned int comm )
{
  if ( comm )
  {
    //  
    long command;
    //    
    TCHAR buf[_MAX_PATH] = _T("");
    CString filter;
    // "  "
    if ( comm == 1 )
    {
      reference ref = ::ksGetCurrentDocument(0); //  reference  
      if ( ref )
      {
        //    
        int type = ::ksGetDocumentType( ref );   
        
        //      
        filter = GetFilter( type, true /*saveAs*/, &command );
        if ( filter )
        {
          //     -          buf
          if ( ::ksSaveFileT( (LPTSTR)(LPCTSTR)::FindExt( filter ),0, (LPTSTR)(LPCTSTR)filter, buf, _MAX_PATH, false ) 
               && buf [0] )
          {
            //   -  -    
            Convert( NULL/*inputFile*/, buf, command/*command*/, TRUE /*showParam*/ );
          }
        }
        else
          ::LibMessage( IDS_ERROR_DOC_TYPE );
      }
      else
        ::LibMessage( IDS_ERR_DOC );
    }
    else // " "
    {
      //   ( )
      filter = GetFilter( -1, false /*saveAs*/, &command );
      
      if ( ::ksChoiceFileT( 0, (LPTSTR)(LPCTSTR)filter, buf, _MAX_PATH, false ) && buf [0] )
      {
        //      
        GetFilter( ::FindExt(buf), false /*saveAs*/, &command );
        //   - 
        Convert( buf/*inputFile*/, NULL /*outFile*/, command/*command*/, TRUE /*showParam*/ );
      }
    }
  }
}


//-------------------------------------------------------------------------------
//        
// ---
BSTR LibConverter::GetFilter( LPCTSTR ext, BOOL saveAs, long * command )
{
  long docType = -1;
  //   
  if ( _tcsicmp( ext, ::FindExt(::LoadStr(IDS_DOC_M3D))) )
    docType = lt_DocPart3D;
  else
    if ( _tcsicmp( ext, ::FindExt(::LoadStr(IDS_DOC_A3D))) )
      docType = lt_DocAssemble3D;
    else
      if ( _tcsicmp( ext, ::FindExt(::LoadStr(IDS_DOC_CDW))) )
        docType = lt_DocSheetStandart;
      else
        if ( _tcsicmp( ext, ::FindExt(::LoadStr(IDS_DOC_FRW))) )
          docType = lt_DocFragment;
        else
          if ( _tcsicmp( ext, ::FindExt(::LoadStr(IDS_DOC_SPW))) )
            docType = lt_DocSpc;
          else
            if ( _tcsicmp( ext, ::FindExt(::LoadStr(IDS_DOC_KDW))) )
              docType = lt_DocTxtStandart;

  return GetFilter( docType, saveAs, command );
}


//-------------------------------------------------------------------------------
//        
//        
// [HKEY_LOCAL_MACHINE\SOFTWARE\ASCON\KOMPAS-3D\Converters\LibConverter]
// ---
BSTR LibConverter::GetFilter( long docType, BOOL saveAs, long * command )
{
  CString res;
  if ( command )
  {
    switch ( docType )
    {
      // 
      case lt_DocPart3D        : 
      {
        res = ::LoadStr( IDS_DOC_M3D );  // " (*.mym3d)|*.mym3d|"
        *command = saveAs ? libConv_Exp_m3d : libConv_Imp_m3d;
        break;
      }
      // 
      case lt_DocAssemble3D        : 
      {
        res = ::LoadStr( IDS_DOC_A3D ); // " (*.mya3d)|*.mya3d|"
        *command = saveAs ? libConv_Exp_a3d : libConv_Imp_a3d;
        break;
      }
      //    
      case lt_DocSheetUser     :
      case lt_DocSheetStandart : 
      {
        res = ::LoadStr( IDS_DOC_CDW ); // " (*.mycdw)|*.mycdw|"
        *command = saveAs ? libConv_Exp_cdw : libConv_Imp_cdw;
        break;
      }
      // 
      case lt_DocFragment        : 
      {
        res = ::LoadStr( IDS_DOC_FRW ); // " (*.myfrw)|*.myfrw|";  
        *command = saveAs ? libConv_Exp_frw : libConv_Imp_frw;
        break;
      }
      //    
      case lt_DocSpcUser    :
      case lt_DocSpc        : 
      {
        res = ::LoadStr( IDS_DOC_SPW ); // " (*.myspw)|*.myspw|";  
        *command = saveAs ? libConv_Exp_spw : libConv_Imp_spw;
        break;
      }
      //      
      case lt_DocTxtUser       :
      case lt_DocTxtStandart   : 
      {
        res = ::LoadStr( IDS_DOC_KDW ); // "  (*.mykdw)|*.mykdw|";  
        *command = saveAs ? libConv_Exp_kdw : libConv_Imp_kdw;
        break;
      }
      //      
      default:
      {
        res  = ::LoadStr( IDS_DOC_M3D );
        res += ::LoadStr( IDS_DOC_A3D );
        res += ::LoadStr( IDS_DOC_CDW );
        res += ::LoadStr( IDS_DOC_FRW );
        res += ::LoadStr( IDS_DOC_SPW );
        res += ::LoadStr( IDS_DOC_KDW );
        *command = -1;
        break;
      }
    }
  }
  return res.AllocSysString();
}


//-----------------------------------------------------------------------------
//      
// ---
void LibConverter::SetSaveLineStyle( bool val )
{
  lineStyle = val;
}

//-----------------------------------------------------------------------------
//      
// ---
bool LibConverter::GetSaveLineStyle()
{
  return lineStyle;
}
