/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

package ipipan.poliqarp.gui;

import java.util.Iterator;

import ipipan.poliqarp.logic.Match;
import ipipan.poliqarp.logic.Query;
import ipipan.poliqarp.logic.results.*;

public class PlainTxtResultWriter extends ResultWriter 
{
   public PlainTxtResultWriter(Configuration cfg)
   {
      super(cfg);
   }

   private int[] columnWidth;
   private ColumnType[] header;

   /**
    * Calculates the length of the longest string in each result column.
    */ 
   private void calculateColumnWidths(Query query)
   {
      columnWidth = new int[numCols];
      int i;

      for (i = 0; i < numCols; i++) // probably not needed,
         columnWidth[i] = 0;        // but I'm not sure
      
      for (Iterator<Match> iter = query.getMatchList().getIterator();
         iter.hasNext(); )
      {
         Match match = iter.next();
         for (i = 0; i < numCols; i++) {
            int len = match.getCell(i).toString(cfg).length();
            if (columnWidth[i] < len)
               columnWidth[i] = len;
         }
      }
   }

   protected void writeHeader(Query query, String encoding) 
   {
      writer.println(cfg.getComponentName("TEXT_RESULTS") + " " + query);
   }

   private void writeField(String x, int columnWidth, boolean right) 
   {
      int width;
      if (x == null) 
         width = 0;
      else 
         width = x.length();

      if (!right & x != null) 
         writer.write(x);
      for (int k = width; k < columnWidth; k++)
         writer.write(' ');
      if (right & x != null) 
         writer.write(x);
   }

   protected void writeMatch(Match match)
   {
      boolean right;
      for (int i = 0; i < numCols; i++) {
         if(numCols == 6)  // FIXME
            right = (i > 1); // FIXME
         else 
            // FIXME - align should be a ColumnType property
            right = (header[i] instanceof ColumnLeftContext ||
               header[i] instanceof ColumnLeftMatch);
         writeField(match.getCell(i).toString(cfg), columnWidth[i] + 1, right);
      }
      writer.println();
   }

   protected void writeMatches(Query query)
   {
      calculateColumnWidths(query);
      header = query.getHeader();
      super.writeMatches(query);
   }
}
