package com.eactive.eai.common.util.convert;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.eactive.eai.common.util.Logger;

public class IConv {
    protected static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

    private static final char[] ASCII_TO_EBCDIC_TABLE = {
        0x0000, 0x0001, 0x0002, 0x0003, 0x0037, 0x002d, 0x002e, 0x002f, 0x0016,
        0x0005, 0x0025, 0x000b, 0x000c, 0x000d, 0x003f, 0x003f, 0x0010, 0x0011,
        0x0012, 0x0013, 0x003c, 0x003d, 0x0032, 0x0026, 0x0018, 0x0019, 0x003f,
        0x0027, 0x0007, 0x001d, 0x001e, 0x001f, 0x0040, 0x005a, 0x007f, 0x007b,
        0x005b, 0x006c, 0x0050, 0x007d, 0x004d, 0x005d, 0x005c, 0x004e, 0x006b,
        0x0060, 0x004b, 0x0061, 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5,
        0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x007a, 0x005e, 0x004c, 0x007e, 0x006e,
        0x006f, 0x007c, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
        0x00c8, 0x00c9, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
        0x00d8, 0x00d9, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8,
        0x00e9, 0x0070, 0x00e0, 0x0080, 0x00b0, 0x006d, 0x0079, 0x0081, 0x0082,
        0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x0091, 0x0092,
        0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x00a2, 0x00a3,
        0x00a4, 0x00a5, 0x00a6, 0x00a7, 0x00a8, 0x00a9, 0x00c0, 0x004f, 0x00d0,
        0x00a1, 0x003f, 0x004a, 0x005f, 0x00b2, 0x00a0, 0x006a, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
        0x003f, 0x003f, 0x003f, 0x003f
    };
    private static final char[] EBCDIC_TO_ASCII_TABLE = {
        0x0000, 0x0001, 0x0002, 0x0003, 0x007F, 0x0009, 0x007F, 0x001C, 0x007F,
        0x007F, 0x007F, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011,
        0x0012, 0x0013, 0x007F, 0x007F, 0x0008, 0x007F, 0x0018, 0x0019, 0x007F,
        0x007F, 0x001A, 0x001D, 0x001E, 0x001F, 0x007F, 0x007F, 0x007F, 0x007F,
        0x007F, 0x000A, 0x0017, 0x001B, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F,
        0x0005, 0x0006, 0x0007, 0x007F, 0x007F, 0x0016, 0x007F, 0x007F, 0x007F,
        0x007F, 0x0004, 0x007F, 0x007F, 0x007F, 0x007F, 0x0014, 0x0015, 0x007F,
        0x007F, 0x0020, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F,
        0x007F, 0x007F, 0x0080, 0x002E, 0x003C, 0x0028, 0x002B, 0x007C, 0x0026,
        0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F,
        0x0021, 0x0024, 0x002A, 0x0029, 0x003B, 0x0081, 0x002D, 0x002F, 0x007F,
        0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x0084, 0x002C,
        0x0025, 0x005F, 0x003E, 0x003F, 0x005B, 0x007F, 0x007F, 0x007F, 0x007F,
        0x007F, 0x007F, 0x007F, 0x007F, 0x0060, 0x003A, 0x0023, 0x0040, 0x0027,
        0x003D, 0x0022, 0x005D, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066,
        0x0067, 0x0068, 0x0069, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F,
        0x007F, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, 0x0071,
        0x0072, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x0083, 0x007E,
        0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007F,
        0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x005E, 0x007F, 0x0082, 0x007F,
        0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F,
        0x007F, 0x007F, 0x007F, 0x007B, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045,
        0x0046, 0x0047, 0x0048, 0x0049, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F,
        0x007F, 0x007D, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050,
        0x0051, 0x0052, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x005C,
        0x007F, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A,
        0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x007F, 0x0030, 0x0031, 0x0032,
        0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x007F, 0x007F,
        0x007F, 0x007F, 0x007F, 0x007F
    };

    //  94 X 94 table.. 1st - 161 invalid ranged value : X'FFFF'
    private static char[][] DB_ASCII_TO_EBCDIC_TABLE = new char[94][94];

    // 149 X 190 .. 1st - 65 Out of range valuie : X'FF'
    private static char[][] DB_EBCDIC_TO_ASCII_TABLE = new char[149][190];
//    private static boolean initialized = false;
    private static final byte SO = 0x0E;
    private static final byte SI = 0x0F;

    static {
        try {
            buildConvTable();
//            initialized = true;
        } catch (Exception e) {
            e.printStackTrace();
//            initialized = false;
        }
    }

    public static byte[] asciiToEBCDIC(String value) {
        int length = value.length();
        int j = 0;
        int WORD = 0;
        boolean isDBCS = false;
        byte[] bytes = new byte[(value.getBytes().length * 5) / 3];
        byte[] dbcs = new byte[2];
        char wc = 0;

        for (int i = 0; i < length; i++) {
            // one byte ALPHANUMERIC 
            if ((value.charAt(i) >> 8) == 0) {
                if (isDBCS) {
                    bytes[j++] = SI;
                    isDBCS = false;
                }

                bytes[j++] = (byte) ASCII_TO_EBCDIC_TABLE[(int) value.charAt(i)];

                continue;
            }

            if (!isDBCS) {
                bytes[j++] = SO;
                isDBCS = true;
            }

            System.arraycopy(value.substring(i, i + 1).getBytes(), 0, dbcs, 0, 2);
            WORD = Integer.parseInt(bytes2Hex(dbcs), 16);

            if ((WORD < 0xA1A1) || (WORD > 0xFEFE)) { // Out of range for 2 bytes character set like hangul
                bytes[j++] = (byte) 0xFF;
                bytes[j++] = (byte) 0xFF;

                continue;
            }

            wc = DB_ASCII_TO_EBCDIC_TABLE[(WORD >> 8) - 161][(WORD & 0x00FF) -
                161];
            bytes[j++] = (byte) (wc >> 8);
            bytes[j++] = (byte) (wc & 0x00FF);
        }

        if (isDBCS) {
            bytes[j++] = SI;
        }

        byte[] _obytes = new byte[j];
        System.arraycopy(bytes, 0, _obytes, 0, j);

        return _obytes;
    }

    public static String ebcdicToASCII(byte[] abytes) {
        int length = abytes.length;
        int j = 0;
        int WORD = 0;
        boolean isDBCS = false;
        byte[] bytes = new byte[length];
        byte[] dbcs = new byte[2];
        char wc = 0;

        for (int i = 0; i < length; i++) {
            // DBCS start ot End  SO-SI
            if (abytes[i] == SO) {
                isDBCS = true;

                continue;
            } else if (abytes[i] == SI) {
                isDBCS = false;

                continue;
            }

            if (((length - i) < 2) && isDBCS) {
                isDBCS = false;
            }

            // one byte ALPHANUMERIC 
            if (!isDBCS) {
                dbcs[0] = 0x00;
                dbcs[1] = abytes[i];
                bytes[j++] = (byte) EBCDIC_TO_ASCII_TABLE[Integer.parseInt(bytes2Hex(
                            dbcs), 16)];

                continue;
            }

            dbcs[0] = abytes[i++];
            dbcs[1] = abytes[i];
            WORD = Integer.parseInt(bytes2Hex(dbcs), 16);

            if (WORD == 0x4040) {
                bytes[j++] = (byte) 0xA1;
                bytes[j++] = (byte) 0xA1;

                continue;
            }

            if (WORD == 0x0000) {
                bytes[j++] = (byte) 0x00;
                bytes[j++] = (byte) 0x00;

                continue;
            }

            if ((WORD < 0x4141) || (WORD > 0xD5FF) || ((WORD & 0x00FF) < 0x41) ||
                    ((WORD & 0x00FF) > 0xFE)) { // Out of range for 2 bytes character set like hangul
                bytes[j++] = (byte) 0xFF;
                bytes[j++] = (byte) 0xFF;

                continue;
            }

            wc = DB_EBCDIC_TO_ASCII_TABLE[(WORD >> 8) - 65][(WORD & 0x00FF) -
                65];
            bytes[j++] = (byte) (wc >> 8);
            bytes[j++] = (byte) (wc & 0x00FF);
        }

        byte[] _obytes = new byte[j];
        System.arraycopy(bytes, 0, _obytes, 0, j);

        return new String(_obytes);
    }

    public static String bytes2Hex(byte[] abyte0) {
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < abyte0.length; i++) {
            buffer.append(Integer.toHexString((abyte0[i] & 0xf0) >> 4)).append(Integer.toHexString(abyte0[i] &
                    0xf));
        }

        return buffer.toString().toUpperCase();
    }

    public static byte[] hex2Bytes(String hex) {
        byte[] buffer = new byte[hex.length() / 2];

        int bIndex = 0;

        for (int i = 0; i < hex.length(); i += 2) {
            buffer[bIndex++] = (byte) (((Integer.parseInt(Character.toString(
                        hex.charAt(i)), 16) & 0xf) << 4) |
                (Integer.parseInt(Character.toString(hex.charAt(i + 1)), 16) &
                0xf));
        }

        return buffer;
    }

    private static void buildConvTable() {
        try {
//            String OS = System.getProperty("os.name");

            String className = IConv.class.getName();
            String pathName = className.substring(0, className.lastIndexOf('.'))
                                       .replace('.', '/');

            BufferedReader br = new BufferedReader(new InputStreamReader(
                        IConv.class.getClassLoader().getResourceAsStream(pathName +
                            "/iconv.tab")));
            String lbuff = br.readLine();
            int bucket = 0;

            while (lbuff != null) {
                if (lbuff.substring(0, 3).equals("*0x")) {
                    for (int i = 0; i < 94; i++)
                        DB_ASCII_TO_EBCDIC_TABLE[bucket][i] = (char) Integer.parseInt(lbuff.substring(22 +
                                    (i * 5), 26 + (i * 5)), 16);

                    bucket++;
                    lbuff = br.readLine();

                    if (lbuff.substring(0, 3).equals("*X'")) {
                        bucket = 0;
                    }

                    continue;
                } else {
                    for (int i = 0; i < 190; i++)
                        DB_EBCDIC_TO_ASCII_TABLE[bucket][i] = (char) Integer.parseInt(lbuff.substring(22 +
                                    (i * 5), 26 + (i * 5)), 16);

                    bucket++;
                }

                lbuff = br.readLine();
            }
             // End of while

            br.close();
        } catch (IOException ex) {
            //System.err.println("IOException caught in readFile");
            //CommonMessages.logGenericTrace("*IConv class has exception in conversion table I/O:" + ex.getMessage());
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        IConv.asciiToEBCDIC("AAA");
        logger.debug("I'm now exit.");
        System.exit(0);
    }
}
