
public class BluetoothReceipt extends AppCompatActivity {


    private static Context context;
    public static PrinterInstance printer;

    private static int ServiceID;

    private static String TransactionId, Date, Time,
            Amount, TotalAmount, MerchantId, ServiceProviderAr,
            ServiceNameAr, ServiceProviderEn, ServiceNameEn, RepeatedCopy,
            ServiceDescriptionAr, ServiceDescriptionEn, Lang;

    private static int SERVICE_ID, TRANSACTION_ID, MERCHANT_ID;
    private static String TIME, AMOUNT, TOTAL_AMOUNT, SERVICE_NAME, DESECRATION, POWERD_BY, REPEATED_COPY, LANG;

    private static List<PaymentModel.ResponseEntity> INFORMATION;
    static Map<String, String> INPUTS_MAP;

    private static String serviceDescription, repeated_copy;


    private static String provider, transaction, time, recieptType, parameters, amount, tAmount;
    private static String align;
    private static String keyAr, keyEn, valueAr, valueEn, key, value;
    private static String serial;

    private static String line = "-------------------------------------------------------------";

    private static String egpay;
    private static String egpaySite = "Tel:01228682403";
    //    private static String egpayTel = "Tel: +202 22620330 | +202 22620331";
    private static String egpayTel = "     01228682405";
//    private static String poweredBy = "Powered By Bee";

    private static SimpleDateFormat _12HourSDF;
    private static Date _24HourDt;

    private DatabaseHelper mydb;
    private int CATEGORY_ID;

    // The Main Constructor Contain All the data need in Receipt Printing
    public BluetoothReceipt(Context mContext,
                            PrinterInstance mPrinter,
                            int serviceId,
                            int transactionId,
                            int merchantId,
                            String time,
                            String amount,
                            String totalAmount,
                            String powerdBy,
                            List<PaymentModel.ResponseEntity> information,
                            String serviceName,
                            Map<String, String> inputsMap,
                            String description,
                            String repeatedCopy,
                            String lang, int categoryId) {


        context = mContext;
        printer = mPrinter;
        SERVICE_ID = serviceId;
        TRANSACTION_ID = transactionId;
        MERCHANT_ID = merchantId;
        TIME = time;
        AMOUNT = amount;
        TOTAL_AMOUNT = totalAmount;
        POWERD_BY = powerdBy;
        INFORMATION = information;
        SERVICE_NAME = serviceName;
        INPUTS_MAP = inputsMap;
        DESECRATION = description;
        REPEATED_COPY = repeatedCopy;
        LANG = lang;
        CATEGORY_ID = categoryId;
        initReceiptData(LANG);



        if (CATEGORY_ID == 62)
            printPrepaidCardBLUETOOTH(context, LANG);
        else
            printBLUETOOTH(context, LANG);

    }

    /**
     * init the receipt data for use it in printBLUETOOTH() and printPrepaidCardBLUETOOTH()
     *
     * @param lang
     */
    private void initReceiptData(String lang) {


        if (lang == "ar") {
            align = "Right";
            provider = SERVICE_NAME;
            transaction = "رقم العملية  : " + TRANSACTION_ID;
            time = "الوقت  : " + TIME;
            recieptType = "عملية ناجحة";
            amount = "المبلغ المطلوب  : " + AMOUNT + " ج.م";
            tAmount = "المبلغ الكلى  : " + TOTAL_AMOUNT + " ج.م";
            serviceDescription = DESECRATION;
            repeated_copy = "نسخة مكررة";
            egpay = "شكرا لاستخدامكم رؤية";
        } else {

            align = "Left";
            provider = SERVICE_NAME;
            transaction = "Transaction id  : " + TRANSACTION_ID;
            time = "Time  : " + TIME;
            recieptType = "Successful Transaction";
            amount = "Amount  : " + AMOUNT + " EGP";
            tAmount = "TotalAmount  : " + TOTAL_AMOUNT + " EGP";
            serviceDescription = DESECRATION;
            repeated_copy = "Repeated Copy";
            egpay = "Thanks for using Rahtek";

        }

    }

    /**
     * Used to print invoices
     *
     * @param context
     * @param lang
     */
    public static void printBLUETOOTH(Context context, String lang) {

        printer.init();
        printer.setFont(0, 0, 0, 0);

        //printing Header
        //printing Logo
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.theka_try);
        printer.printImage(bitmap);

        //printing Body
        //printing provider and services
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 10, false, "", "Center"), 20);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, provider, "Center"), 3);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, line, "Center"), 1);

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, transaction, align), 1);


        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, time, align), 1);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, line, "Center"), 1);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, true, recieptType, "Center"), 3);// كانت ترو وغيرتها فولص
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, line, "Center"), 1);

        //printing parameters which been in inquiry at the first
        printParams(lang);

        //Printing Transaction details
        if (INFORMATION != null) {

            boolean isLooped = false;

            for (int i = 0; i < INFORMATION.size(); i++) {
                key = INFORMATION.get(i).getKey();
                value = INFORMATION.get(i).getValue();

                if (!(value.trim().equals(""))) {
                    isLooped = true;
                    String keyValue = key + " : " + value;
                    if (keyValue.length() <= 35) {
                        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, keyValue, align), 1);
                    } else {

                        int currentLenth = 0;
                        String newText = "";
                        Boolean isDone = false;

                        String[] lines = keyValue.split(" ");

                        for (String oneLine : lines) {

                            int wLen = oneLine.length();
                            if (currentLenth + wLen >= 30 && isDone == false) {
                                newText += "\n" + oneLine;
                                isDone = true;
                            } else {
                                newText += " " + oneLine;
                            }
                            currentLenth += wLen;
                        }

                        String[] newlines = newText.split("\n");
                        for (String oneLine : newlines) {

                            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370,
                                    35, 22, false, oneLine, align), 1);

                        }

                    }

                }
            }
            if (isLooped) {
                printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, line, "Center"), 1);
            }

        }

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, amount, align), 1);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, tAmount, align), 1);

        //Printing  description
        if (!(serviceDescription == null) && !(serviceDescription == "")) {
            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, line, "Center"), 1);
            String[] lines = serviceDescription.split("\n");
            for (String oneLine : lines) {
                printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 20, false, oneLine.trim(), align), 1);
            }

        }

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, line, "Center"), 1);

        //Printing Footer
        //printing repeated copy text if printing for second time
        if (REPEATED_COPY != "") {
            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 250, 35, 25, false, repeated_copy, "Center"), 3);

        }

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, true, context.getString(R.string.merchant_name) + " : " + PreferencesHelper.getMerchantName(context), "Center"), 3);

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, true, egpay, "Center"), 3);

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 10, false, "", "Center"), 20);
        printer.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);
    }

    /**
     * Used to print Prepaid cards only
     *
     * @param context
     * @param lang
     *
     */
    public static void printPrepaidCardBLUETOOTH(Context context, String lang) {
        int serialSize;
        printer.init();
        printer.setFont(0, 0, 0, 0);

        //printing Header
        //printing Logo
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.theka_try);
        printer.printImage(bitmap);

        //printing Body
        //printing provider and services
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 10, false, "", "Center"), 20);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, provider, "Center"), 3);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, line, "Center"), 1);

        //Printing Prepaid Card Code
        if (INFORMATION != null) {
            for (int i = 0; i < INFORMATION.size(); i++) {
                key = INFORMATION.get(i).getKey();
                value = INFORMATION.get(i).getValue();

                if (!(value.trim().equals(""))) {

                    //Getting Pin code and print it here
                    if (key.trim().toLowerCase().equals("pin")) {
                        if (lang == "ar") {
                            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, "رقم كارت الشحن", "Center"), 3);
                        } else {
                            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 22, false, key, "Center"), 3);
                        }
                        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, true, value, "Center"), 3);


//                        printer.printImage(PrintBluetoothUtils.creatBarcode(value, 370, 35), 0);


                    }

                    //Getting Serial number but print it later
                    if ((key.trim().toLowerCase().equals("serial number")) || (key.trim().toLowerCase().equals("serial"))) {
                        if (lang == "ar") {

                          printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, "الرقم المسلسل", "Center"), 3);
                        } else {
                     //       serial = key + " : " + value;
                            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, key, "Center"), 3);

                        }

                        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, true, value, "Center"), 3);
                        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, line, "Center"), 1);

                    }

                }
            }

        }

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, amount, align), 1);

        //printing total amount only if it different from amount
        if (!(AMOUNT.equals(TOTAL_AMOUNT))) {
            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, tAmount, align), 1);

        }

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, transaction, align), 1 );
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, time, align), 1);


        //Printing Prepaid card description
        if (!(serviceDescription == null) && !(serviceDescription == "")) {
            // printer.printImage(PrintUtils.createImageForBluetoothPrinter(context, 370, 35, 25, false, line, "Center"), 1);
            String[] lines = serviceDescription.split("\n");
            for (String oneLine : lines) {
                printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 20, false, oneLine.trim(), align), 1);
            }

        }

        //Printing Footer
        //printing repeated copy text if printing for second time
        if (REPEATED_COPY != "") {
            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 250, 35, 25, false, repeated_copy, "Center"), 3);

        }
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, true, context.getString(R.string.merchant_name) + " : " + PreferencesHelper.getMerchantName(context), "Center"), 3);
        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 25, true, egpay, "Center"), 3);

        printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context, 370, 35, 10, false, "", "Center"), 20);
        printer.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);


    }

    private static void printParams(String lang) {

        // getting data from database
        List<ParamModel> getParamDataFromDataBase = new ArrayList<ParamModel>();

        //Database instance
        DatabaseHelper mDbHelper = DatabaseHelper.getInstance(context);
        SQLiteDatabase database = mDbHelper.getReadableDatabase();
        Cursor GroupCursorPayment = null;
        try {

            if (INPUTS_MAP != null && INPUTS_MAP.size() > 0) {


                for (Object obj : INPUTS_MAP.entrySet()) {
                    Map.Entry<String, String> entry = (Map.Entry) obj;

                    //get key
                    String key = entry.getKey();
                    //get value corresponding to key
                    String value = entry.getValue();

                    getParamDataFromDataBase.add(new ParamModel(0, key, key, value));
                }


                if (getParamDataFromDataBase != null && getParamDataFromDataBase.size() > 0) {
                    for (int i = 0; i < getParamDataFromDataBase.size(); i++) {
                        if (PreferencesHelper.getPrintType(context).equals(0)) {

                            if (lang == "ar") {

                                Bidi bidi = new Bidi(getParamDataFromDataBase.get(i).getName_ar(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
                                if (bidi.getBaseLevel() == 0)
                                    align = "Left"; //en
                                else
                                    align = "Right"; //ar

                                //align = "Right";
                                parameters = getParamDataFromDataBase.get(i).getName_ar() + " : " + getParamDataFromDataBase.get(i).getValue();
                            } else {
                                Bidi bidi = new Bidi(getParamDataFromDataBase.get(i).getName_ar(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
                                if (bidi.getBaseLevel() == 0)
                                    align = "Left"; //en
                                else
                                    align = "Right"; //ar

                                //align = "Left";
                                parameters = getParamDataFromDataBase.get(i).getName_en() + " : " + getParamDataFromDataBase.get(i).getValue();

                            }

                            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context,
                                    370, 35, 25, false, parameters
                                    , align), 1);
                            printer.printImage(PrintBluetoothUtils.createImageForBluetoothPrinter(context,
                                    370, 35, 25, false, line, "Center"), 1);
                        }


                    }
                }


            }


        } finally {
            if (GroupCursorPayment != null) {
                GroupCursorPayment.close();
            }
        }


    }
