/*
 * LogWriter.java
 *
 * Created on 13 Èþëü 2010 ã., 13:25
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.shtrih.util;

/**
 *
 * @author V.Kravtsov
 */

import java.text.*;
import org.apache.log4j.*;
import com.shtrih.fiscalprinter.*;
import com.shtrih.fiscalprinter.command.*;

public class LogWriter {
 
    private static String LogSeparator = 
        "------------------------------------------------------------";
    private static String[] BoolToStr = {"[ ]", "[X]"};
    private static String[] BatteryToStr = {"[ > 80% ]", "[ < 80% ]"};
    private static String[] RecordToStr = {"[ correct ]", "[ corrupted ]"};
    private static String[] DayOpenedToStr = {"[ closed ]", "[ opened ]"};
    private static String[] DayOverToStr = {"[ < 24 hours ]", "[ > 24 hours ]"};
    private static String[] PointToStr = {"[0 digits]", "[2 digits]"};
    private static String[] LeverToStr = {"[up]", "[down]"};
    private static String[] ClosedToStr = {"[closed]", "[opened]"};
    
    private static Logger logger = Logger.getLogger(LogWriter.class);
    
    /** Creates a new instance of LogWriter */
    public LogWriter() {
    }
    
    public static void writeSeparator()
    {
        logger.debug(LogSeparator);
    }
    
    public static void write(DeviceMetrics data)
    {
        logger.debug(LogSeparator);
        logger.debug("Printer parameters");
        logger.debug(LogSeparator);
        logger.debug("Device type                    : " +
            String.valueOf(data.getDeviceType()) + "." + 
            String.valueOf(data.getDeviceSubType()));
        
        logger.debug("Protocol version               : " +
            String.valueOf(data.getProtocolVersion()) + "." + 
            String.valueOf(data.getProtocolSubVersion()));
        
        logger.debug("Model ID                       : " + 
            String.valueOf(data.getModel()));
        
        logger.debug("Device language                : " + 
            String.valueOf(data.getLanguage()));
        
        logger.debug("Device name                    : " + 
            data.getDeviceName());
    }

    public static int boolToInt(boolean value)
    {
        if (value) return 1;
        else return 0;
    }
        
    public static void write(FiscalMemoryFlags flags)
    {
        logger.debug(LogSeparator);
        logger.debug("FM flags                       : 0x" +
            Hex.toHex(flags.getValue()));
        
        logger.debug(LogSeparator);
        logger.debug("FM memory bank1                : " + 
            BoolToStr[boolToInt(flags.isFm1Present())]);
        
        logger.debug("FM memory bank2                : " + 
            BoolToStr[boolToInt(flags.isFm2Present())]);
        
        logger.debug("License present                : " + 
            BoolToStr[boolToInt(flags.isLicensePresent())]);
        
        logger.debug("FM overflow                    : " + 
            BoolToStr[boolToInt(flags.isFmOverflow())]);
        
        logger.debug("FM battery state               : " + 
            BatteryToStr[boolToInt(flags.isBatteryLow())]);
        
        logger.debug("Last FM record                 : " + 
            RecordToStr[boolToInt(flags.isFmLastRecordCorrupted())]);
        
        logger.debug("FM day                         : " + 
            DayOpenedToStr[boolToInt(flags.isDayOpened())]);
        
        logger.debug("Day end required               : " + 
            DayOverToStr[boolToInt(flags.isEndDayRequired())]);
    }
    
    public static void write(PrinterFlags flags)
    {
        logger.debug(LogSeparator);
        logger.debug(
            "Printer flags                  : 0x" +
            Hex.toHex(flags.getValue()));
        
        logger.debug(LogSeparator);
        logger.debug("Journal paper near end         : " + 
            BoolToStr[boolToInt(flags.isRecNearEnd())]);
        
        logger.debug("Receipt paper near end         : " + 
            BoolToStr[boolToInt(flags.isRecNearEnd())]);
        
        logger.debug("Slip upper sensor              : " + 
            BoolToStr[boolToInt(flags.isSlpEmpty())]);
        
        logger.debug("Slip low sensor                : " + 
            BoolToStr[boolToInt(flags.isSlpNearEnd())]);
        
        logger.debug("Amount decimal point position  : " + 
            PointToStr[boolToInt(flags.getAmountPointPosition())]);
        
        logger.debug("Electronic journal present     : " + 
            BoolToStr[boolToInt(flags.isEJPresent())]);
        
        logger.debug("Journal paper                  : " + 
            BoolToStr[boolToInt(flags.isJrnEmpty())]);
        
        logger.debug("Receipt paper                  : " + 
            BoolToStr[boolToInt(flags.isRecEmpty())]);
        
        logger.debug("Journal lever                  : " + 
            LeverToStr[boolToInt(flags.isJrnLeverUp())]);
        
        logger.debug("Receipt lever                  : " + 
            LeverToStr[boolToInt(flags.isRecLeverUp())]);
        
        logger.debug("Cover status                   : " + 
            ClosedToStr[boolToInt(flags.isCoverOpened())]);
        
        logger.debug("Cash drawer                    : " + 
            ClosedToStr[boolToInt(flags.isDrawerOpened())]);
        
        logger.debug("EJ is near end                 : " + 
            BoolToStr[boolToInt(flags.isEJNearEnd())]);
    }
    
    public static void write(ShortPrinterStatus status)
    {
        logger.debug(LogSeparator);
        logger.debug("Short printer status");
        logger.debug(LogSeparator);
        
        logger.debug("FM result code                 : " +
            String.valueOf(status.getFMResultCode()));

        logger.debug("EJ result code                 : " + 
            String.valueOf(status.getEJResultCode()));
        
        logger.debug("Receipt operations             : " + 
            String.valueOf(status.getReceiptOperations()));
        
        logger.debug("Battery voltage                : " +
            String.valueOf(status.getBatteryVoltage()) + " V");
        
        
        NumberFormat formatter = new DecimalFormat("#0.00");
        logger.debug("Power voltage                  : " + 
            formatter.format(status.getPowerVoltage()) + " V");
        
        logger.debug("Operator number                : " + 
            String.valueOf(status.getOperatorNumber()));
    }
    
    public static void write(LongPrinterStatus status)
    {
        logger.debug(LogSeparator);
        logger.debug("Long printer status");
        logger.debug(LogSeparator);
        logger.debug("Operator number                : " +  
            String.valueOf(status.getOperatorNumber()));
        
        logger.debug(
            "Printer firmware version       : " + 
            status.getFirmwareRevision());

        logger.debug("Printer store number           : " + 
            String.valueOf(status.getLogicalNumber()));
        
        logger.debug("Document number                : " + 
            String.valueOf(status.getDocumentNumber()));
        
        logger.debug("Printer mode                   : " + 
            String.valueOf(status.getMode()) + ", " +
            status.getPrinterMode().getText());
        
        logger.debug("Printer submode                : " + 
            String.valueOf(status.getSubmode()) + ", " +
            status.getPrinterSubmode().getText());
        
        logger.debug("Printer internal port number   : " +
            String.valueOf(status.getPortNumber()));
        
        logger.debug("FM firmware                    : " + 
            status.getFMFirmwareVersion() + "." + 
            String.valueOf(status.getFMFirmwareBuild()) + ", " + 
            status.getFMFirmwareDate().toString());
        
        logger.debug("Printer date and time          : " + 
            status.getDate().toString() + ", " + 
            status.getTime().toString());
        
        logger.debug("Serial number                  : " + 
            String.valueOf(status.getSerialNumber()));
        
        logger.debug("Day number                     : " + 
            String.valueOf(status.getDayNumber()));
        
        logger.debug("FM free records                : " + 
            String.valueOf(status.getFMFreeRecords()));
        
        logger.debug("Registration number            : " + 
            String.valueOf(status.getRegistrationNumber()));
        
        logger.debug("Fiscalizations left            : " + 
            String.valueOf(status.getFreeRegistration()));
        
        logger.debug("Fiscal ID                      : " + 
            status.getFiscalIDText());
        
        write(status.getPrinterFlags());
        write(status.getFiscalMemoryFlags());
    }
}
