/***************************************************************************
                          xls_write.cpp  -  description
                             -------------------
    begin                : Thu Apr 14 2004
    copyright            : (C) 1999-2006 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#if __BORLANDC__ > 0x500
#include <condefs.h>
#endif

#include <stdio.h>

#include <sptk3/CStrings.h>
#include <sptk3/CDateTime.h>
#include <sptk3/CException.h>
#include <sptk3/excel/CExcelBook.h>
#include <sptk3/excel/CExcelCell.h>

#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif

using namespace std;
using namespace sptk;

int main(int argc, char *argv[]) {

   char dirname[128];
   getcwd(dirname,128);
   printf("Current dir: %s\n",dirname);

   try {
      CExcelBook  book("org");

      book.init();

      unsigned fonts[6];
      fonts[0] = book.defineFont("Arial",12);
      fonts[1] = book.defineFont("Arial",12,EFM_ATTR_ITALIC);
      fonts[2] = book.defineFont("Arial",12,EFM_ATTR_BOLD);
      fonts[3] = book.defineFont("Arial",12,EFM_ATTR_BOLD|EFM_ATTR_ITALIC);
      fonts[4] = book.defineFont("Arial",12,EFM_ATTR_UNDERLINE);
      fonts[5] = book.defineFont("Arial",12,EFM_ATTR_STRIKEOUT);


#define MAX_COLORS 56 // maximum colors in palette for Excel is 56

      CExcelCellFormat *fmts[MAX_COLORS];

      /// Can define up to 56 possible palette colors
      unsigned maxColors = MAX_COLORS;  
      unsigned c = 0;           // format index
      for (unsigned r = 0x80; r <= 0xFF; r += 0x18) {
         for (unsigned g = 0x80; g <= 0xFF; g += 0x18) {
            for (unsigned b = 0x80; b <= 0xFF; b += 0x18) {
               if (c >= maxColors) {
                  r = g = b = 0xFF;
                  break;
               }
               unsigned color = (r * 256 + g) * 256 + b;
               unsigned colorIndex = book.palette.colorIndex(color);

               std::string formatName = "Custom" + int2string(c);
               CExcelCellFormat *f = book.defineFormat(
                     fonts[c%6],  // font index
                     0,           // numeric format index
                     CAF_CENTER,  // align
                     false,       // text wrap
                     CBL_DOUBLE,  // border
                     1,           // dotted pattern
                     colorIndex,  // pattern fg color
                     colorIndex   // pattern bg color
                  );
               fmts[c] = f;
               c++;
            }
         }
      }

      if (c < maxColors) maxColors = c;

      CExcelSheet *sheet = book.newSheet("Sheet 1");

#define MAX_ROWS 100
#define MAX_COLS 100

      sheet->resize(MAX_ROWS,128);

      FILE *csv = fopen("Book1.csv","wb");

      unsigned cn = 0;
      for (unsigned row = 0; row < MAX_ROWS; row++) {
         for (unsigned column = 0; column < MAX_COLS; column++) {
            CExcelCell& cell1 = sheet->rows[row][column];
            if (column == 0)
               cell1.setString("Row "+ int2string(row));
            else  cell1.setInteger(cn);
            cell1.format(fmts[cn%maxColors]);
            if (column == 0)
               fprintf(csv,"%i",cn);
            else fprintf(csv,",%i",cn);
            cn++;
         }
         fprintf(csv,"\n");
      }
      //sheet1.format(xfr,0,0,2,2);
      fclose(csv);

      book.writeExcelFile("Book2.xls");
      book.readExcelFile("Book2.xls");
   }
   catch (std::exception& e) {
      puts(e.what());
   }
   return 0;
}
