/*
 * MainMenu.java
 *
 * Created on 26 Íîÿáðü 2009 ã., 15:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fptrtest;

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


import jpos.*;
import java.io.*;
import java.util.Vector;
import com.shtrih.fiscalprinter.*;
import com.shtrih.jpos.fiscalprinter.*;
import com.shtrih.fiscalprinter.command.*;

public class MainMenu {
    
    private final ConsoleUtil console = new ConsoleUtil();
    private final ShtrihFiscalPrinter printer = new ShtrihFiscalPrinter();
    
    /** Creates a new instance of MainMenu */
    public MainMenu() {
    }
    
    public void addCommand(MenuItems menuItem, FlexCommands commands, 
        int code)
    {
        FlexCommand command = commands.itemByCode(code);
        if (command != null)
        {
            CommandMenuItem item = new CommandMenuItem(console, command, printer);
            menuItem.add(item);
        }
    }
    
    public Vector createMenu()
    throws Exception
    {
        FlexCommands commands = new FlexCommands();
        FlexCommandsReader reader = new FlexCommandsReader();
        reader.loadFromXml("commands.xml", commands);
            
        MenuItems menuItem;
        Vector result = new Vector();
        // Query commands
        menuItem = new MenuItems("Query commands", console);
        addCommand(menuItem, commands, 0x11);
        addCommand(menuItem, commands, 0x10);
        addCommand(menuItem, commands, 0x15);
        addCommand(menuItem, commands, 0x1A);
        addCommand(menuItem, commands, 0x1B);
        addCommand(menuItem, commands, 0x26);
        addCommand(menuItem, commands, 0xFC);
        result.add(menuItem);
        
        // Tax inspector commands
        menuItem = new MenuItems("Tax inspector commands", console);
        addCommand(menuItem, commands, 0x65);
        addCommand(menuItem, commands, 0x1D);
        addCommand(menuItem, commands, 0x1C);
        addCommand(menuItem, commands, 0x69);
        addCommand(menuItem, commands, 0x0D);
        addCommand(menuItem, commands, 0x0E);
        addCommand(menuItem, commands, 0x0F);
        result.add(menuItem);
        
        // Print commands
        menuItem = new MenuItems("Print commands", console);
        addCommand(menuItem, commands, 0x17);
        addCommand(menuItem, commands, 0x12);
        addCommand(menuItem, commands, 0x18);
        addCommand(menuItem, commands, 0x25);
        addCommand(menuItem, commands, 0x29);
        addCommand(menuItem, commands, 0x2F);
        addCommand(menuItem, commands, 0x52);
        addCommand(menuItem, commands, 0x53);
        addCommand(menuItem, commands, 0x54);
        addCommand(menuItem, commands, 0xB0);
        addCommand(menuItem, commands, 0xC0);
        addCommand(menuItem, commands, 0xC1);
        addCommand(menuItem, commands, 0xC2);
        addCommand(menuItem, commands, 0xC3);
        addCommand(menuItem, commands, 0xC4);
        addCommand(menuItem, commands, 0xC5);
        addCommand(menuItem, commands, 0xE1);
        addCommand(menuItem, commands, 0xE2);
        addCommand(menuItem, commands, 0xE3);
        addCommand(menuItem, commands, 0xE4);
        result.add(menuItem);
        
        // Receipt commands
        menuItem = new MenuItems("Receipt commands", console);
        addCommand(menuItem, commands, 0x50);
        addCommand(menuItem, commands, 0x51);
        addCommand(menuItem, commands, 0x80);
        addCommand(menuItem, commands, 0x81);
        addCommand(menuItem, commands, 0x82);
        addCommand(menuItem, commands, 0x83);
        addCommand(menuItem, commands, 0x84);
        addCommand(menuItem, commands, 0x85);
        addCommand(menuItem, commands, 0x86);
        addCommand(menuItem, commands, 0x87);
        addCommand(menuItem, commands, 0x88);
        addCommand(menuItem, commands, 0x89);
        addCommand(menuItem, commands, 0x8A);
        addCommand(menuItem, commands, 0x8B);
        addCommand(menuItem, commands, 0x8C);
        addCommand(menuItem, commands, 0x8D);
        result.add(menuItem);
        
        // Report commands
        menuItem = new MenuItems("Report commands", console);
        addCommand(menuItem, commands, 0x40);
        addCommand(menuItem, commands, 0x41);
        addCommand(menuItem, commands, 0x42);
        addCommand(menuItem, commands, 0x43);
        addCommand(menuItem, commands, 0x66);
        addCommand(menuItem, commands, 0x67);
        result.add(menuItem);
        
        // EJ commands
        menuItem = new MenuItems("EJ commands", console);
        addCommand(menuItem, commands, 0xA0);
        addCommand(menuItem, commands, 0xA1);
        addCommand(menuItem, commands, 0xA2);
        addCommand(menuItem, commands, 0xA3);
        addCommand(menuItem, commands, 0xA4);
        addCommand(menuItem, commands, 0xA5);
        addCommand(menuItem, commands, 0xA6);
        addCommand(menuItem, commands, 0xA7);
        addCommand(menuItem, commands, 0xA8);
        addCommand(menuItem, commands, 0xA9);
        addCommand(menuItem, commands, 0xAA);
        addCommand(menuItem, commands, 0xAB);
        addCommand(menuItem, commands, 0xAC);
        addCommand(menuItem, commands, 0xAD);
        addCommand(menuItem, commands, 0xAE);
        addCommand(menuItem, commands, 0xAF);
        addCommand(menuItem, commands, 0xB1);
        addCommand(menuItem, commands, 0xB2);
        addCommand(menuItem, commands, 0xB3);
        addCommand(menuItem, commands, 0xB4);
        addCommand(menuItem, commands, 0xB5);
        addCommand(menuItem, commands, 0xB6);
        addCommand(menuItem, commands, 0xB7);
        addCommand(menuItem, commands, 0xB8);
        addCommand(menuItem, commands, 0xB9);
        addCommand(menuItem, commands, 0xBA);
        addCommand(menuItem, commands, 0xBB);
        addCommand(menuItem, commands, 0xBC);
        result.add(menuItem);
        
        // Slip commands
        menuItem = new MenuItems("Slip commands", console);
        addCommand(menuItem, commands, 0x2A);
        addCommand(menuItem, commands, 0x70);
        addCommand(menuItem, commands, 0x71);
        addCommand(menuItem, commands, 0x72);
        addCommand(menuItem, commands, 0x73);
        addCommand(menuItem, commands, 0x74);
        addCommand(menuItem, commands, 0x75);
        addCommand(menuItem, commands, 0x76);
        addCommand(menuItem, commands, 0x77);
        addCommand(menuItem, commands, 0x78);
        addCommand(menuItem, commands, 0x79);
        addCommand(menuItem, commands, 0x7A);
        addCommand(menuItem, commands, 0x7B);
        addCommand(menuItem, commands, 0x7C);
        addCommand(menuItem, commands, 0x7D);
        addCommand(menuItem, commands, 0x7E);
        addCommand(menuItem, commands, 0xE1);
        result.add(menuItem);
        
        // Table commands
        menuItem = new MenuItems("Table commands", console);
        addCommand(menuItem, commands, 0x1E);
        addCommand(menuItem, commands, 0x1F);
        addCommand(menuItem, commands, 0x24);
        addCommand(menuItem, commands, 0x2D);
        addCommand(menuItem, commands, 0x2E);
        result.add(menuItem);
        
        // Fiscal memory commands
        menuItem = new MenuItems("FM commands", console);
        addCommand(menuItem, commands, 0x61);
        addCommand(menuItem, commands, 0x62);
        addCommand(menuItem, commands, 0x63);
        addCommand(menuItem, commands, 0x64);
        result.add(menuItem);
        
        // Misc. commands
        menuItem = new MenuItems("Misc. commands", console);
        addCommand(menuItem, commands, 0x13);
        addCommand(menuItem, commands, 0x16);
        addCommand(menuItem, commands, 0x19);
        addCommand(menuItem, commands, 0x20);
        addCommand(menuItem, commands, 0x21);
        addCommand(menuItem, commands, 0x22);
        addCommand(menuItem, commands, 0x23);
        addCommand(menuItem, commands, 0x27);
        addCommand(menuItem, commands, 0x28);
        addCommand(menuItem, commands, 0x2B);
        addCommand(menuItem, commands, 0x2C);
        addCommand(menuItem, commands, 0x60);
        addCommand(menuItem, commands, 0x68);
        addCommand(menuItem, commands, 0xC8);
        addCommand(menuItem, commands, 0xC9);
        addCommand(menuItem, commands, 0xCA);
        addCommand(menuItem, commands, 0xF0);
        addCommand(menuItem, commands, 0xF1);
        addCommand(menuItem, commands, 0xF3);
        addCommand(menuItem, commands, 0xFD);
        addCommand(menuItem, commands, 0xE0);
        result.add(menuItem);
        
        // Table commands
        menuItem = new MenuItems("Table commands", console);
        menuItem.add(new ReadTablesMenuItem(console, printer));
        menuItem.add(new WriteTablesMenuItem(console, printer));
        result.add(menuItem);
        
        return result;
    }
    
    public void execute()
    throws Exception
    {
        // set codepage
        try
        {
         System.setOut(new CodepagePrintStream(
             System.out,System.getProperty("console.encoding","Cp866")) );
        }
        catch(UnsupportedEncodingException e)
        {
         System.out.println("Unable to setup console codepage: " + e);
        }
        
        System.out.println("************************************************************");
        System.out.println("*                                                          *");
        System.out.println("*        Fiscal printer test utility, version 1.0          *");
        System.out.println("*                       SHTRIH-M 2009                      *");
        System.out.println("*                                                          *");
        System.out.println("************************************************************");
        
        String deviceName = console.readLine("Enter JPOS device name to open: ");
        System.out.print("Open device...");
        printer.open(deviceName);
        System.out.println("OK");
        System.out.println("Device service version: " + 
            printer.getDeviceServiceVersion());
        System.out.println("Device service description: " + 
            printer.getDeviceServiceDescription());
        
        System.out.print("Claiming device...");
        printer.claim(0);
        System.out.println("OK");
        
        System.out.print("Enabling device...");
        printer.setDeviceEnabled(true);
        System.out.println("OK");
            
        Vector menuItems = createMenu();
        while (true)
        {
            console.printSeparator();
            // show commands menu
            for (int i=0;i<menuItems.size();i++)
            {
                MenuItemInterface item = (MenuItemInterface)menuItems.get(i);
                String line = String.valueOf(i+1) + ") " + item.getText();
                System.out.println(line);
            }
            String line = String.valueOf(menuItems.size()+1) + ") Exit";
            System.out.println(line);
            
            int number = console.readMenuItem();
            if (number == menuItems.size()+1) {
                printer.close();
                return;
            }
            if ((number > 0)&&(number <= menuItems.size()))
            {
                MenuItemInterface item = (MenuItemInterface)menuItems.get(number-1);
                item.execute();
            }
        }
    }
    
    
}
