package com.printer.sdk.usb

import android.app.Activity
import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.*
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.*
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import com.vipul.hp_hp.library.Layout_to_Image
import de.hdodenhof.circleimageview.CircleImageView
import etelligens.com.foodsafety.R
import etelligens.com.foodsafety.activities.dashboard.SharedHelper
import etelligens.com.foodsafety.activities.lables.frontEnd.CustomLabelTypeFrontEnd
import etelligens.com.foodsafety.activities.lables.frontEnd.GoGrabProductListFrontEnd
import etelligens.com.foodsafety.activities.lables.frontEnd.LabelTypesFrontEnd
import etelligens.com.foodsafety.activities.lables.frontEnd.MasterLabelCategoryFrontEnd
import etelligens.com.foodsafety.activities.storage.frontend.ChooseStorageAreaFrontEnd
import etelligens.com.foodsafety.adapter.CustomLabelFormatForPrintAdapter
import etelligens.com.foodsafety.adapter.GoGrabLableFormetAdapter
import etelligens.com.foodsafety.model.CustomLabelFormatForPrintModel
import etelligens.com.foodsafety.model.GoGrabLableFormetModel
import etelligens.com.foodsafety.utils.BaseActivity
import etelligens.com.foodsafety.utils.EndPoint
import etelligens.com.foodsafety.utils.Keyword
import etelligens.com.foodsafety.utils.Tools
import kotlinx.android.synthetic.main.activity_main.*
import org.apache.commons.lang3.StringUtils
import org.json.JSONException
import org.json.JSONObject
import java.net.URLDecoder
import java.net.URLEncoder
import java.text.Normalizer
import java.text.SimpleDateFormat
import java.util.*

class MainActivityChinese :BaseActivity() {
    lateinit var DBuserData: SharedPreferences
    private var printing: Printing? = null
    var loginToken2: String? = null
    var currentLanguage: String? = null
    private val TAG: String = "MainActivityChinese";
    private var productName: String = "";
    private var date: String = ""
    var idArray: java.util.ArrayList<String>? = null
    var outletName: java.util.ArrayList<String?>? = null
    private var temprature: String = ""
    private var tempDate: String = ""
    private var tempType: String = ""
    private var isLogoPrint: Int = 0
    private var isLogoSelect: Int = 0
    private var isBarcodeSelect: Int = 0
    private var day: String = ""
    private var userName2: String = ""
    private var bitmap: String = ""
    private var image: String = ""
    var currentDateTimeString: String? = null
    var dayOfTheWeek: String? = null
    var labelName: String? = null
    var createdAt: String? = null
    var rolename: String? = null
    var barcode_img_path: String? = null
    var d = Date()
    var sdf = SimpleDateFormat("hh:mm a")
    var day2 = SimpleDateFormat("EEEE")
    private var finalPrint: String = "FinalPrint"
    private var description = ""
    private var progress: ProgressDialog? = null
    var pID: String? = null
    var oID: String? = null
    var time: String? = null
    var Ingredient: String? = null
    var Allergens: String? = null
    var enjoy_by: String? = null
    var quantity: String? = null
    var namee: String? = null
    var tempMax: String? = null
    var tempMin: String? = null
    var note: String? = null
    var source: String? = null

    var vendor_logo: String? = null
    var models = ArrayList<CustomLabelFormatForPrintModel>()
    var cutomLabelFormatForPrint: CustomLabelFormatForPrintAdapter? = null
    val getAllName = ArrayList<String>()
    var productlogoimg: CircleImageView? = null
    var enjoybytxt: TextView? = null
    var label_name_txt: TextView? = null
    var ownerName: TextView? = null
    var tvTemp: TextView? = null
    var tvTempDate: TextView? = null
    var created_at_txt: TextView? = null
    var add_created_at_txt: TextView? = null
    var curent_time: TextView? = null
    var tempRange: TextView? = null
    var current_day: TextView? = null
    var nametxt: TextView? = null
    var prodcutPrictxt: TextView? = null
    var ingredientstxt: TextView? = null
    var allergenstxt: TextView? = null
    var notesText: TextView? = null
    var goGrabLableFormetModels = ArrayList<GoGrabLableFormetModel>()
    var goGrabLableFormetAdapter: GoGrabLableFormetAdapter? = null
    var sendDataName: CustomLabelFormatForPrintAdapter.sendDataName = CustomLabelFormatForPrintAdapter.sendDataName { }
    var recyclerView: RecyclerView? = null
    var gradGoLayout: RelativeLayout? = null
    var useFirstLayout: LinearLayout? = null
    var CustomLabelsLayout: LinearLayout? = null
    var llTemp: LinearLayout? = null
    var prepLabelsLayout: LinearLayout? = null
    var recyclearViewusedata: RecyclerView? = null
    var layout_to_image: Layout_to_Image? = null
    lateinit var bitmapImage: Bitmap
    var cutomlevellogo: ImageView? = null
    var useFirst_Image: ImageView? = null
    var image_view: ImageView? = null
    var tempImage: ImageView? = null
    var image_custom: ImageView? = null
    var barcode_image: ImageView? = null
    var barcodeImage_GoGrab: ImageView? = null
    var passArrayList = GoGrabLableFormetAdapter.passArrayList { }
    var GranArray = ArrayList<String>()
    var list: ArrayList<PrintModel> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        FinalPrint.init(this)
        getselectedLanguage()
        initViews()
        initListeners()
        DBuserData = getSharedPreferences(Keyword.DB_LOGIN_USER_DATA, Context.MODE_PRIVATE)
        loginToken2 = DBuserData.getString("LoginToken", null).toString()
        if (progress == null) {
            progress = ProgressDialog(this, R.style.progressTheme)
            progress!!.setTitle(R.string.please_wait)
        }
        dayOfTheWeek = day2.format(d)
        currentDateTimeString = sdf.format(d)
        val i = intent
        if (i != null) {

            date = i.getStringExtra("Date").toString()
            image = i.getStringExtra("Image").toString()
            source = i.getStringExtra("source").toString()
            finalPrint = i.getStringExtra("FinalPrint").toString()
            barcode_img_path = i.getStringExtra("barcode_img_path").toString()
            time = i.getStringExtra("time").toString()
            vendor_logo = i.getStringExtra("vendor_logo").toString()
        }
        gradGoLayout = findViewById(R.id.gradGoLayout)
        notesText = findViewById(R.id.notesText)
        barcodeImage_GoGrab = findViewById(R.id.barcodeImage_GoGrab)
        recyclerView = findViewById(R.id.add_new_recyclerview)
        productlogoimg = findViewById(R.id.product_logo_img)
        enjoybytxt = findViewById(R.id.enjoy_edtxt)
        nametxt = findViewById(R.id.product_name_txt)
        prodcutPrictxt = findViewById(R.id.product_rate_txt)
        ingredientstxt = findViewById(R.id.ingredients_txt)
        allergenstxt = findViewById(R.id.allergens_txt)
        CustomLabelsLayout = findViewById(R.id.CustomLabelsLayout)
        useFirstLayout = findViewById(R.id.useFirstLayout)
        useFirst_Image = findViewById(R.id.useFirst_Image)
        tempRange = findViewById(R.id.tvTempRange)
        prepLabelsLayout = findViewById(R.id.prepLabelsLayout)
        recyclearViewusedata = findViewById(R.id.recyclearView_use_data)
        cutomlevellogo = findViewById(R.id.cutom_level_logo)
        label_name_txt = findViewById(R.id.label_name_txt)
        created_at_txt = findViewById(R.id.created_at_txt)
        add_created_at_txt = findViewById(R.id.add_created_at_txt)
        image_view = findViewById(R.id.image_view)
        barcode_image = findViewById(R.id.barcode_image)
        ownerName = findViewById(R.id.ownerName)
        image_custom = findViewById(R.id.image_custom)
        llTemp = findViewById(R.id.lltempView)
        tvTemp = findViewById(R.id.tvTemp)
        tvTempDate = findViewById(R.id.tvDate)
        tempImage = findViewById(R.id.temp_image_view)

        if (source.equals("isComingFromTemp")) {
            llTemp?.visibility = View.VISIBLE
            val i = intent
            bitmap = i.getStringExtra("bitmap").toString()
            tempDate = i.getStringExtra("tempDate").toString()
            tempType = i.getStringExtra("tempType").toString()
            temprature = i.getStringExtra("temprature").toString()
            tempMax = i.getStringExtra("maxTemp").toString()
            tempMin = i.getStringExtra("minTemp").toString()

            tempImage?.let {
                Glide.with(applicationContext)
                    .load(EndPoint.ETE_IMAGE_BASEURL + bitmap)
                    .into(it)
            }
            //  Log.e("imagedata", EndPoint.ETE_IMAGE_BASEURL + bitmap)
            tempRange?.setText("Temp Range=" + tempMin + " " + tempType + " " + "To" + " " + tempMax + " " + tempType)
            tvTemp?.setText("Temprature= " + temprature + " " + tempType)
            tvTempDate?.setText("Date= " + tempDate)

            // printTempData(bitmap,tempDate,tempType,temprature)
        }
        if (finalPrint.equals("GoGrabLableFormet")) {
            gradGoLayout?.visibility = View.VISIBLE
            val i = intent
            oID = i.getStringExtra("oID")
            pID = i.getStringExtra("pID")
            currentLanguage=i.getStringExtra("language")
            vendor_logo = i.getStringExtra("vendor_logo")
            getgogradetail()

        } else if (finalPrint.equals("CustomLabelFormet")) {
            CustomLabelsLayout?.visibility = View.VISIBLE
            val i = intent
            if (i != null) {
                oID = i.getStringExtra("oID")
                pID = i.getStringExtra("pID")
                vendor_logo = i.getStringExtra("vendor_logo")
                // image = i.getStringExtra("Image")!!
                image_custom?.let {
                    Glide.with(applicationContext)
                        .load(EndPoint.ETE_IMAGE_BASEURL + "/" + vendor_logo)
                        .into(it)
                }
                customLabelLineFormat()
            }

        } else if (finalPrint.equals("FinalPrint")) {
            prepLabelsLayout?.visibility = View.VISIBLE
            val i = intent
            if (i != null) {
                userName2 = i.getStringExtra("rolename")!!
                productName = i.getStringExtra("name")!!
                oID = i.getStringExtra("oID")
                pID = i.getStringExtra("pID")
                image = i.getStringExtra("Image").toString()
                image_view?.let {
                    Glide.with(applicationContext)
                        .load(EndPoint.ETE_IMAGE_BASEURL + "/" + vendor_logo)
                        .into(it)
                }
                barcode_image?.let {
                    Glide.with(applicationContext)
                        .load(EndPoint.ETE_IMAGE_BASEURL + barcode_img_path).into(it)
                }
                label_name_txt?.setText(productName + "\n")
                created_at_txt?.setText(/*dayOfTheWeek+ " "+*/time + "\n")
                add_created_at_txt?.setText(date + "\n")
//                curent_time?.setText(currentDateTimeString)
                current_day?.setText(dayOfTheWeek)
                ownerName?.setText(userName2)
            }
        } else if (finalPrint.equals("UseFirstLabelFormat")) {
            useFirstLayout?.visibility = View.VISIBLE
            useFirst_Image?.let {
                Glide.with(applicationContext).load(
                    SharedHelper.getKey(
                        this,
                        "venderLogo"
                    )
                )
                    .into(it)
            }
        }
        layout_to_image = Layout_to_Image(this, gradGoLayout)
        //now call the main working function ;) and hold the returned image in bitmap
        //now call the main working function ;) and hold the returned image in bitmap
        bitmapImage = layout_to_image!!.convert_layout()

    }

    private fun printTempData(
        image: String,
        tempDate: String,
        tempType: String,
        temprature: String
    ) {
        val printables = ArrayList<Printable>().apply {

            Picasso.get().load(EndPoint.ETE_IMAGE_BASEURL + image.substring(1))
                .into(object : Target {
                    override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                        progress?.dismiss()
                        var scaledBitmap: Bitmap? = scaleDown(bitmap, 150.0f, true)
                        var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                        add(ImagePrintable.Builder(bit).build())
                        progress?.dismiss()
                        add(
                            TextPrintable.Builder()
                                .setText(getString(R.string.temp_range) + tempMin + " " + tempType + " " + "To" + " " + tempMax + " " + tempType + "\n")
                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                .build()
                        )
                        add(
                            TextPrintable.Builder()
                                .setText(getString(R.string.temprature_txtinventory) +"= " + temprature + " " + tempType + "\n")
                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                .build()
                        )
                        add(
                            TextPrintable.Builder()
                                .setText(getString(R.string.date) +"= " + tempDate + "\n")
                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                .setNewLinesAfter(3)
                                .build()
                        )
                    }

                    override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {
                        progress?.dismiss()
                        Log.e("bitmapError=", e.toString())
                    }

                    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                        progress?.dismiss()
                    }
                })
        }
        Log.e(TAG, "Printingg: " + printables)
        if (!printables.isEmpty()) {
            printing?.print(printables)
        } else {
            Log.e(TAG, "no data found: " + printables)
        }
        /* }, 5000)*/

        //finish()
    }

    override fun onResume() {
        super.onResume()
        if (FinalPrint.hasPairedPrinter())
            printing = FinalPrint.printer()
    }

    private fun initViews() {
        btnPiarUnpair.text =
            if (FinalPrint.hasPairedPrinter()) "Un-pair ${FinalPrint.getPairedPrinter()?.name}" else "Pair with printer"
    }

    override fun onBackPressed() {
        super.onBackPressed()
        if (source.equals("isComingFromTemp")) {
            val intent = Intent(this, ChooseStorageAreaFrontEnd::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity(intent)
            finish()
        } else if (finalPrint.equals("UseFirstLabelFormat")){
            val intent = Intent(this, MasterLabelCategoryFrontEnd::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity(intent)
            finish()
        }else if (finalPrint.equals("FinalPrint")){
            val intent = Intent(this, LabelTypesFrontEnd::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity(intent)
            finish()
        }else if (finalPrint.equals("GoGrabLableFormet")){

            val intent = Intent(this, GoGrabProductListFrontEnd::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity(intent)
            finish()
        }else if (finalPrint.equals("CustomLabelFormet")){

            val intent = Intent(this, CustomLabelTypeFrontEnd::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity(intent)
            finish()
        }
    }

    private fun initListeners() {

        btnPrintImages.setOnClickListener {
            if (!FinalPrint.hasPairedPrinter()) startActivityForResult(
                Intent(
                    this,
                    ScanningActivity::class.java
                ),
                ScanningActivity.SCANNING_FOR_PRINTER
            )
            else {
                if (source.equals("isComingFromTemp")) {

                    Handler(Looper.getMainLooper()).post {
                        printTempData(bitmap, tempDate, tempType, temprature)
                    }


                } else if (finalPrint.equals("UseFirstLabelFormat")) {
                    var text =
                        StringUtils.stripAccents(getString(R.string.tv_use)+"\n\n" +getString(R.string.tv_first)+"\n\n")
                    SharedHelper.getKey(this, "venderLogo")
                    printSomePrintable(text)

                } else if (finalPrint.equals("GoGrabLableFormet")) {
                    gradGoLayout?.visibility = View.VISIBLE
                    val i = intent
                    oID = i.getStringExtra("oID")
                    pID = i.getStringExtra("pID")

                    if (isBarcodeSelect == 1 && isLogoSelect == 1) {
                        val printables = ArrayList<Printable>().apply {
                            Log.e(TAG, "Printables: " + printing.toString())
                            Picasso.get().load(
                                EndPoint.ETE_IMAGE_BASEURL + vendor_logo?.substring(1)
                            )
                                .into(object : Target {
                                    override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                                        progress?.dismiss()
                                        var scaledBitmap: Bitmap? = scaleDown(bitmap, 150.0f, true)
                                        var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                                        add(ImagePrintable.Builder(bit).build())
                                        progress?.dismiss()
                                        Log.e("grabData= ", GranArray.toString())
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(namee) + "\n" + quantity + "\n" + getString(R.string.enjoy_by) + StringUtils.stripAccents(enjoy_by) + "\n")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                                .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .build()
                                        )

                                        for (i in 0 until GranArray.size) {
                                            Log.e("jsndvsnlbs: ", "" + list.get(i).name)

                                            if (list.get(i).parent_id.equals("562") && list.get(i).name.equals(
                                                    R.string.daily_value
                                                )
                                            ) {
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.equals(R.string.nutrition_fact) ||
                                                list.get(i).name.equals(R.string.calories) ||
                                                list.get(i).name.equals(R.string.total_fat) ||
                                                list.get(i).name.equals(R.string.cholesterol) ||
                                                list.get(i).name.equals(R.string.sodium) ||
                                                list.get(i).name.equals(R.string.total_carbo) ||
                                                list.get(i).name.equals(R.string.protein) /*||
                                            list.get(i).name.equals("Carbohydrate")*/

                                            ) {
                                                if (list.get(i).name.length == 15) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 9) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "         ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 11) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "       ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 12) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "      ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 7) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "           ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 6) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "            ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 8) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "          ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).zero.length == 10) {

                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "     ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                            .build()
                                                    )
                                                }
                                            } else if (list.get(i).name.length == 15) {
                                                Log.e("15called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "   ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 14) {
                                                Log.e("14called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "    ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 13) {
                                                Log.e("13called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "     ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 12) {
                                                Log.e("12called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "      ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 11) {
                                                Log.e("11called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "       ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 10) {
                                                Log.e("10called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "        ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 9) {
                                                Log.e("9called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "         ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 8) {
                                                Log.e("8called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "          ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 7) {
                                                Log.e("7called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "           ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 6) {
                                                Log.e("6called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "            ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 5) {
                                                Log.e("5called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "             ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 4) {
                                                Log.e("4called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "              ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 3) {
                                                Log.e("3called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "               ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            }
                                            if (list.get(i).parent_id.equals("562") && list.get(i).name.equals(
                                                    "Daily Value"
                                                )
                                            ) {

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        .build()
                                                )

                                            } else if (list.get(i).zero.length == 1) {
                                                Log.e("zero1called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "         ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 4 && list.get(i).zero.equals(
                                                    "null"
                                                )
                                            ) {
                                                Log.e("zero4nullcalled--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 2) {
                                                Log.e("zero2called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "        ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 3) {
                                                Log.e("zero3called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "       ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 4) {
                                                Log.e("zero4called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "      ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 5) {
                                                Log.e("zero5called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "     ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 10) {
                                                Log.e("zero5called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "       ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            }

                                            if (list.get(i).parent_id.equals("4")) {
                                                Log.e("id4called--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("  \n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).parent_id.equals("600")
                                            ) {
                                                Log.e("elsecalled--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("  \n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).parent_id.equals("562") && list.get(
                                                    i
                                                ).name.equals(
                                                    getString(R.string.daily_value)
                                                )
                                            ) {
                                                Log.e("elsecalled--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("%Daily  %Value\n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_RIGHT)
                                                        .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                        .build()
                                                )
                                            } else {
                                                Log.e("elsecalled--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).one) + "% \n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            }
                                        }

                                        add(
                                            TextPrintable.Builder()
                                                .setText(getString(R.string.percent_daily_value) + StringUtils.stripAccents(note) + getString(R.string.calorie_diet) + "\n")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )

                                        add(
                                            TextPrintable.Builder()
                                                .setText(getString(R.string.ingredients) + StringUtils.stripAccents(Ingredient) + "\n" + getString(
                                                    R.string.allergens) + StringUtils.stripAccents(Allergens) + "\n\n")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )


                                        Picasso.get()
                                            .load(EndPoint.ETE_IMAGE_BASEURL + barcode_img_path)
                                            .into(object : Target {
                                                override fun onBitmapLoaded(
                                                    bitmap: Bitmap,
                                                    from: Picasso.LoadedFrom
                                                ) {
                                                    var scaledBitmap: Bitmap? =
                                                        scaleDown(bitmap, 350.0f, true)
                                                    var bit: Bitmap =
                                                        scaledBitmap?.let { toGrayscale(it) }!!
                                                    add(ImagePrintable.Builder(bit).build())
                                                }

                                                override fun onBitmapFailed(
                                                    e: Exception,
                                                    errorDrawable: Drawable?
                                                ) {
                                                }

                                                override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
                                            })

                                        add(
                                            TextPrintable.Builder()
                                                .setText("  \n")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                .build()
                                        )

                                    }


                                    override fun onBitmapFailed(
                                        e: Exception,
                                        errorDrawable: Drawable?
                                    ) {
                                        progress?.dismiss()
                                    }

                                    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                                        progress?.dismiss()
                                    }
                                })
                        }
                        Log.e(TAG, "Printingg: " + printables)
                        printing?.print(printables)

                    } else if (isLogoSelect == 1 && isBarcodeSelect == 0) {
                        val printables = ArrayList<Printable>().apply {
                            Log.e(TAG, "Printables: " + printing.toString())
                            Picasso.get().load(
                                EndPoint.ETE_IMAGE_BASEURL + vendor_logo?.substring(1)
                            )
                                .into(object : Target {
                                    override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                                        progress?.dismiss()
                                        var scaledBitmap: Bitmap? = scaleDown(bitmap, 150.0f, true)
                                        var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                                        add(ImagePrintable.Builder(bit).build())
                                        progress?.dismiss()
                                        Log.e("grabData= ", GranArray.toString())
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(namee) + "\n" + StringUtils.stripAccents(quantity) + "\n" + getString(R.string.enjoy_by) + StringUtils.stripAccents(enjoy_by) + "\n")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                                .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .build()
                                        )

                                        for (i in 0 until GranArray.size) {
                                            Log.e("jsndvsnlbs: ", "" + list.get(i).name)

                                            if (list.get(i).parent_id.equals("562") && list.get(i).name.equals(
                                                    getString(R.string.daily_value)
                                                )
                                            ) {
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.equals(getString(R.string.nutrition_fact)) ||
                                                list.get(i).name.equals(getString(R.string.calories)) ||
                                                list.get(i).name.equals(getString(R.string.total_fat)) ||
                                                list.get(i).name.equals(getString(R.string.cholesterol)) ||
                                                list.get(i).name.equals(getString(R.string.sodium)) ||
                                                list.get(i).name.equals(getString(R.string.total_carbo)) ||
                                                list.get(i).name.equals(getString(R.string.protein)) /*||
                                            list.get(i).name.equals("Carbohydrate")*/

                                            ) {
                                                if (list.get(i).name.length == 15) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 9) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "         ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 11) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "       ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 12) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "      ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 7) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "           ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 6) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "            ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).name.length == 8) {
                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).name) + "          ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                            .build()
                                                    )
                                                } else if (list.get(i).zero.length == 10) {

                                                    add(
                                                        TextPrintable.Builder()
                                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "     ")
                                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                            .build()
                                                    )
                                                }
                                            } else if (list.get(i).name.length == 15) {
                                                Log.e("15called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "   ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 14) {
                                                Log.e("14called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "    ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 13) {
                                                Log.e("13called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "     ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 12) {
                                                Log.e("12called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "      ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 11) {
                                                Log.e("11called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "       ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 10) {
                                                Log.e("10called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "        ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 9) {
                                                Log.e("9called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "         ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 8) {
                                                Log.e("8called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "          ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 7) {
                                                Log.e("7called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "           ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 6) {
                                                Log.e("6called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "            ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 5) {
                                                Log.e("5called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "             ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 4) {
                                                Log.e("4called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "              ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).name.length == 3) {
                                                Log.e("3called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).name) + "               ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            }
                                            if (list.get(i).parent_id.equals("562") && list.get(i).name.equals(
                                                    getString(R.string.daily_value)
                                                )
                                            ) {

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        .build()
                                                )

                                            } else if (list.get(i).zero.length == 1) {
                                                Log.e("zero1called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "         ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 4 && list.get(i).zero.equals(
                                                    "null"
                                                )
                                            ) {
                                                Log.e("zero4nullcalled--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 2) {
                                                Log.e("zero2called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "        ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 3) {
                                                Log.e("zero3called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "       ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 4) {
                                                Log.e("zero4called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "      ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 5) {
                                                Log.e("zero5called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "     ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).zero.length == 10) {
                                                Log.e("zero5called--", "called")

                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).zero) + "       ")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            }

                                            if (list.get(i).parent_id.equals("4")) {
                                                Log.e("id4called--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("  \n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).parent_id.equals("600")
                                            ) {
                                                Log.e("elsecalled--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("  \n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            } else if (list.get(i).parent_id.equals("562") && list.get(
                                                    i
                                                ).name.equals(
                                                    getString(R.string.daily_value)
                                                )
                                            ) {
                                                Log.e("elsecalled--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText("%Daily  %Value\n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .setAlignment(DefaultPrinter.ALIGNMENT_RIGHT)
                                                        .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                        .build()
                                                )
                                            } else {
                                                Log.e("elsecalled--", "called")
                                                add(
                                                    TextPrintable.Builder()
                                                        .setText(StringUtils.stripAccents(list.get(i).one) + "% \n")
                                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                        // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                        .build()
                                                )
                                            }
                                        }

                                        add(
                                            TextPrintable.Builder()
                                                .setText(getString(R.string.percent_daily_value) +StringUtils.stripAccents(note) + getString(R.string.calorie_diet) + "\n")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )

                                        add(
                                            TextPrintable.Builder()
                                                .setText(getString(R.string.ingredients) + StringUtils.stripAccents(Ingredient) + "\n" + getString(
                                                    R.string.allergens) +StringUtils.stripAccents(Allergens) + "\n\n")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    }

                                    override fun onBitmapFailed(
                                        e: Exception,
                                        errorDrawable: Drawable?
                                    ) {
                                        progress?.dismiss()
                                    }

                                    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                                        progress?.dismiss()
                                    }
                                })
                        }
                        Log.e(TAG, "Printingg: " + printables)
                        printing?.print(printables)
                    } else if (isLogoSelect == 0 && isBarcodeSelect == 1) {
                        val printables = ArrayList<Printable>().apply {
                            Log.e(TAG, "Printables: " + printing.toString())
                            progress?.dismiss()
                            Log.e("grabData= ", GranArray.toString())
                            add(
                                TextPrintable.Builder()
                                    .setText(StringUtils.stripAccents(namee) + "\n" + StringUtils.stripAccents(quantity) + "\n" +getString(R.string.enjoy_by) + enjoy_by + "\n")
                                    .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                    .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                    .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                    .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                    .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                    .build()
                            )

                            for (i in 0 until GranArray.size) {
                                Log.e("jsndvsnlbs: ", "" + list.get(i).name)

                                if (list.get(i).parent_id.equals("562") && list.get(i).name.equals(
                                        getString(R.string.daily_value)
                                    )
                                ) {
                                    add(
                                        TextPrintable.Builder()
                                            .setText("")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.equals(getString(R.string.nutrition_fact)) ||
                                    list.get(i).name.equals(getString(R.string.calories)) ||
                                    list.get(i).name.equals(getString(R.string.total_fat)) ||
                                    list.get(i).name.equals(getString(R.string.cholesterol)) ||
                                    list.get(i).name.equals(getString(R.string.sodium)) ||
                                    list.get(i).name.equals(getString(R.string.total_carbo)) ||
                                    list.get(i).name.equals(getString(R.string.protein)) /*||
                                            list.get(i).name.equals("Carbohydrate")*/

                                ) {
                                    if (list.get(i).name.length == 15) {
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).name) + "")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    } else if (list.get(i).name.length == 9) {
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).name) + "         ")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    } else if (list.get(i).name.length == 11) {
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).name) + "       ")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    } else if (list.get(i).name.length == 12) {
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).name) + "      ")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    } else if (list.get(i).name.length == 7) {
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).name) + "           ")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    } else if (list.get(i).name.length == 6) {
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).name) + "            ")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    } else if (list.get(i).name.length == 8) {
                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).name) + "          ")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                                .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                                .build()
                                        )
                                    } else if (list.get(i).zero.length == 10) {

                                        add(
                                            TextPrintable.Builder()
                                                .setText(StringUtils.stripAccents(list.get(i).zero) + "     ")
                                                .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                                .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                                //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                                .build()
                                        )
                                    }
                                } else if (list.get(i).name.length == 15) {
                                    Log.e("15called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "   ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 14) {
                                    Log.e("14called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "    ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 13) {
                                    Log.e("13called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "     ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 12) {
                                    Log.e("12called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "      ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 11) {
                                    Log.e("11called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "       ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 10) {
                                    Log.e("10called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "        ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 9) {
                                    Log.e("9called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "         ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 8) {
                                    Log.e("8called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "          ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 7) {
                                    Log.e("7called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "           ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 6) {
                                    Log.e("6called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "            ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 5) {
                                    Log.e("5called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "             ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 4) {
                                    Log.e("4called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "              ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).name.length == 3) {
                                    Log.e("3called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).name) + "               ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                }
                                if (list.get(i).parent_id.equals("562") && list.get(i).name.equals(
                                        "Daily Value"
                                    )
                                ) {

                                    add(
                                        TextPrintable.Builder()
                                            .setText("")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            .build()
                                    )

                                } else if (list.get(i).zero.length == 1) {
                                    Log.e("zero1called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "         ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            .build()
                                    )
                                } else if (list.get(i).zero.length == 4 && list.get(i).zero.equals(
                                        "null"
                                    )
                                ) {
                                    Log.e("zero4nullcalled--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText("")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            .build()
                                    )
                                } else if (list.get(i).zero.length == 2) {
                                    Log.e("zero2called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "        ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).zero.length == 3) {
                                    Log.e("zero3called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "       ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            //.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).zero.length == 4) {
                                    Log.e("zero4called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "      ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).zero.length == 5) {
                                    Log.e("zero5called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "     ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).zero.length == 10) {
                                    Log.e("zero5called--", "called")

                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).zero) + "       ")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                }

                                if (list.get(i).parent_id.equals("4")) {
                                    Log.e("id4called--", "called")
                                    add(
                                        TextPrintable.Builder()
                                            .setText("  \n")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).parent_id.equals("600")
                                ) {
                                    Log.e("elsecalled--", "called")
                                    add(
                                        TextPrintable.Builder()
                                            .setText("  \n")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                } else if (list.get(i).parent_id.equals("562") && list.get(i).name.equals(
                                        getString(R.string.daily_value)
                                    )
                                ) {
                                    Log.e("elsecalled--", "called")
                                    add(
                                        TextPrintable.Builder()
                                            .setText("%Daily  %Value\n")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .setAlignment(DefaultPrinter.ALIGNMENT_RIGHT)
                                            .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                            .build()
                                    )
                                } else {
                                    Log.e("elsecalled--", "called")
                                    add(
                                        TextPrintable.Builder()
                                            .setText(StringUtils.stripAccents(list.get(i).one) + "% \n")
                                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                            // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                            .build()
                                    )
                                }
                            }

                            add(
                                TextPrintable.Builder()
                                    .setText(getString(R.string.percent_daily_value) + StringUtils.stripAccents(note) + getString(R.string.calorie_diet) + "\n")
                                    .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                    .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                    .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                    // .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                                    .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                    .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                    .build()
                            )

                            add(
                                TextPrintable.Builder()
                                    .setText(getString(R.string.ingredients) + StringUtils.stripAccents(Ingredient) + "\n" + getString(
                                        R.string.allergens) + StringUtils.stripAccents(Allergens) + "\n\n")
                                    .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                    .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                    .setAlignment(DefaultPrinter.ALIGNMENT_LEFT)
                                    .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                                    .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON)
                                    .build()
                            )


                            Picasso.get()
                                .load(EndPoint.ETE_IMAGE_BASEURL + barcode_img_path)
                                .into(object : Target {
                                    override fun onBitmapLoaded(
                                        bitmap: Bitmap,
                                        from: Picasso.LoadedFrom
                                    ) {
                                        var scaledBitmap: Bitmap? =
                                            scaleDown(bitmap, 350.0f, true)
                                        var bit: Bitmap =
                                            scaledBitmap?.let { toGrayscale(it) }!!
                                        add(ImagePrintable.Builder(bit).build())
                                    }

                                    override fun onBitmapFailed(
                                        e: Exception,
                                        errorDrawable: Drawable?
                                    ) {
                                    }

                                    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
                                })

                        }
                        Log.e(TAG, "Printingg: " + printables)
                        printing?.print(printables)

                    }
                } else if (finalPrint.equals("FinalPrint")) {
                    Log.e("productname==", productName)
                    Log.e("date==", date.toString())
                    Log.e("dayOfTheWeek==", dayOfTheWeek.toString())
                    Log.e("userName2==", userName2)
                    val encodeName:String=changeString(productName)
                    vendor_logo?.substring(1)?.let { it1 -> Log.e("image==", it1) }
                    printPrebLabels(
                        encodeName,
                        date,
                        time.toString(),
                        dayOfTheWeek.toString(),
                        userName2,
                        vendor_logo?.substring(1).toString()
                    )

                } else if (finalPrint.equals("CustomLabelFormet")) {

                    Log.e(TAG, "images: " + EndPoint.ETE_IMAGE_BASEURL + image?.substring(1))

                    printCustomLabels(
                        getAllName,
                        EndPoint.ETE_IMAGE_BASEURL + image?.substring(1)
                    )
                }
            }
        }

//        btnPiarUnpair.setOnClickListener {
//            if (FinalPrint.hasPairedPrinter()) FinalPrint.removeCurrentPrinter()
//            else startActivityForResult(
//                Intent(this, ScanningActivity::class.java),
//                ScanningActivity.SCANNING_FOR_PRINTER
//            )
//            initViews()
//        }

        /*  btnCustomPrinter.setOnClickListener {
              startActivity(Intent(this, WoosimActivity::class.java))
          }*/

        printing?.printingCallback = object : PrintingCallback {
            override fun connectingWithPrinter() {
                Toast.makeText(this@MainActivityChinese, "Connecting with printer", Toast.LENGTH_SHORT)
                    .show()
            }

            override fun printingOrderSentSuccessfully() {
                Toast.makeText(this@MainActivityChinese, "Order sent to printer", Toast.LENGTH_SHORT)
                    .show()
            }

            override fun connectionFailed(error: String) {
                Toast.makeText(this@MainActivityChinese, "Failed to connect printer", Toast.LENGTH_SHORT)
                    .show()
            }

            override fun onError(error: String) {
                Toast.makeText(this@MainActivityChinese, error, Toast.LENGTH_SHORT).show()
            }

            override fun onMessage(message: String) {
                Toast.makeText(this@MainActivityChinese, "Message: $message", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun changeString(value: String): String {
        return URLEncoder.encode(value, "UTF-8")

    }

    fun customLabelLineFormat() {
        loadershow()
        var url: String? = null
        url = if (currentLang != null) {
            EndPoint.ETE_CUSTOM_PRODUCT_BY_ID + pID + "?language=" + currentLang
        } else {
            EndPoint.ETE_CUSTOM_PRODUCT_BY_ID + pID
        }
        println("$url..........................image")
        val request: StringRequest = object : StringRequest(Method.GET, url,
            Response.Listener { response ->
                println("$response........res..................image")
                Log.e("finalPrinttt", "customLabelLineFormat" + response)
                try {
                    val `object` = JSONObject(response)
                    val msg = `object`.getString("msg")
                    val status = `object`.getInt("status")
                    if (status == 1) {

                        val gson = Gson()
                        val jsonArray = `object`.getJSONArray("data")
                        val jsonObject=jsonArray.getJSONObject(0)
                        image = jsonObject.getString("image")
                        isLogoPrint = jsonObject.getInt("logo_on_label")
                        if (isLogoPrint == 1) {
                            Picasso.get().load(EndPoint.ETE_IMAGE_BASEURL + image.substring(1))
                                .into(image_custom)
                        }

                        val descArray = jsonArray.getJSONArray(1)
                        for (i in 0 until descArray.length()) {
                            val object1 = descArray.getJSONObject(i)
                            description = object1.getString("description")
                            Log.e("data=", description)
                            getAllName.add(StringUtils.stripAccents(description))
                            val model = gson.fromJson(
                                object1.toString(),
                                CustomLabelFormatForPrintModel::class.java
                            )
                            models.add(model)

                        }
                        Log.e("listData", getAllName.toString())

                        cutomLabelFormatForPrint = CustomLabelFormatForPrintAdapter(
                            applicationContext,
                            models,
                            sendDataName
                        )
                        val layoutManager = LinearLayoutManager(
                            applicationContext,
                            LinearLayoutManager.VERTICAL,
                            false
                        )
                        recyclearViewusedata!!.layoutManager = layoutManager
                        recyclearViewusedata?.setAdapter(cutomLabelFormatForPrint)
                        Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                loaderHide()
            }, Response.ErrorListener { error ->
                if (error is TimeoutError || error is NoConnectionError) {
                    Toast.makeText(
                        applicationContext,
                        getString(R.string.internet_connection_fail),
                        Toast.LENGTH_SHORT
                    ).show()
                }
                loaderHide()
            }) {
            @Throws(AuthFailureError::class)
            override fun getHeaders(): Map<String, String> {
                val map: MutableMap<String, String> = HashMap()
                map["Authorization"] = "Bearer " + loginToken2
                println("$map...................image")
                return map
            }
        }
        request.retryPolicy = DefaultRetryPolicy(
            Keyword.MY_SOCKET_TIMEOUT,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
        )
        val requestQueue = Volley.newRequestQueue(applicationContext)
        requestQueue.add(request)
        requestQueue.addRequestFinishedListener<Any> { requestQueue.cache.clear() }
    }

    private fun printSomePrintable(text:String) {
        if (SharedHelper.getKey(
                this@MainActivityChinese,
                "venderLogo"
            ).equals("")
        ) {
            val printable = ArrayList<Printable>().apply {
                add(
                    TextPrintable.Builder()
                        .setText(text)
                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                        .setNewLinesAfter(1)
                        .setFontSize(30)
                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                        .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                        .build()
                )
            }
            printing?.print(printable)
        } else {
            val printables = ArrayList<Printable>().apply {
                Log.e(TAG, "Printables: " + printing.toString())
//            for (i in 0 until data.size) {
                progress?.dismiss()

                Picasso.get().load(
                    SharedHelper.getKey(
                    this@MainActivityChinese,
                    "venderLogo"
                )
                )
                    .into(object : Target {
                        override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                            var scaledBitmap: Bitmap? = scaleDown(bitmap, 150.0f, true)
                            var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                            add(ImagePrintable.Builder(bit).build())
                            add(
                                TextPrintable.Builder()
                                    .setText("先" +"\n" +"用" +"\n\n")
                                    .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                    .setNewLinesAfter(1)
                                    .setFontSize(30)
                                    .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                    .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                    .build()
                            )
                        }

                        override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {}
                        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
                    })
            }
            Log.e(TAG, "Printingg: " + printables)
            printing?.print(printables)
        }
    }

    private fun printPrebLabels(

        productName: String,
        date: String,
        time: String,
        day: String,
        userName: String,
        images: String

    ) {
        val decryptName:String= decodeData(productName)
        val pName:String=StringUtils.stripAccents(decryptName)
        Log.e("prooductName==",pName)
        if (!vendor_logo.equals("") && !barcode_img_path.equals("")) {
            val printables = ArrayList<Printable>().apply {
                Log.e(TAG, "Printables: " + printing.toString())
                val myString = EndPoint.ETE_IMAGE_BASEURL + images
                Log.e(TAG, "ImageUrl: " + myString)
                val data = HashMap<String, String>()
                data.put("Product", productName)
                data.put("Date", date)
                data.put("Time", time)
                data.put("userName", userName)
                data.put("day", day)
                data.put("images", images)
//            for (i in 0 until data.size) {
                progress?.dismiss()
                Log.e("DataFinal ", data.toString())
                Picasso.get().load(EndPoint.ETE_IMAGE_BASEURL + vendor_logo?.substring(1))
                    .into(object : Target {
                        override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                            var scaledBitmap: Bitmap? = scaleDown(bitmap, 100.0f, true)
                            var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                            add(ImagePrintable.Builder(bit).build())

                            add(
                                TextPrintable.Builder()
                                    .setText(pName + "\n" + date + "\n" + time + "\n                   " + StringUtils.stripAccents(userName))
                                    .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                    .setNewLinesAfter(1)
                                    .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                    .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                    .build()
                            )

                            Picasso.get().load(EndPoint.ETE_IMAGE_BASEURL + barcode_img_path)
                                .into(object : Target {
                                    override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                                        var scaledBitmap: Bitmap? = scaleDown(bitmap, 320.0f, true)
                                        var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                                        add(ImagePrintable.Builder(bit).build())
                                    }

                                    override fun onBitmapFailed(
                                        e: Exception,
                                        errorDrawable: Drawable?
                                    ) {
                                    }

                                    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
                                })
                            add(
                                TextPrintable.Builder()
                                    .setText("         " + "\n" + "       " + "\n")
                                    .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                    .setNewLinesAfter(1)
                                    .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                    .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                    .build()
                            )
                        }

                        override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {}
                        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
                    })
            }
            Log.e(TAG, "Printingg: " + printables)
            printing?.print(printables)
        } else if (!vendor_logo.equals("") && barcode_img_path.equals("")) {
            val printables = ArrayList<Printable>().apply {
                Log.e(TAG, "Printables: " + printing.toString())
                val myString = EndPoint.ETE_IMAGE_BASEURL + images
                Log.e(TAG, "ImageUrl: " + myString)
                val data = HashMap<String, String>()
                data.put("Product", productName)
                data.put("Date", date)
                data.put("Time", time)
                data.put("userName", userName)
                data.put("day", day)
                data.put("images", images)
//            for (i in 0 until data.size) {
                progress?.dismiss()
                Log.e("DataFinal ", data.toString())
                Picasso.get().load(EndPoint.ETE_IMAGE_BASEURL + vendor_logo?.substring(1))
                    .into(object : Target {
                        override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                            var scaledBitmap: Bitmap? = scaleDown(bitmap, 100.0f, true)
                            var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                            add(ImagePrintable.Builder(bit).build())

                            add(
                                TextPrintable.Builder()
                                    .setText(pName + "\n" + date + "\n" + dayOfTheWeek + ". " + time + "\n                   " + StringUtils.stripAccents(userName))
                                    .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                    .setNewLinesAfter(1)
                                    .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                    .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                    .build()
                            )
                        }

                        override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {}
                        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
                    })
            }
            Log.e(TAG, "Printingg: " + printables)
            printing?.print(printables)
        } else if (vendor_logo.equals("") && !barcode_img_path.equals("")) {
            val printables = ArrayList<Printable>().apply {
                Log.e(TAG, "Printables: " + printing.toString())
                val data = HashMap<String, String>()
                data.put("Product", productName)
                data.put("Date", date)
                data.put("Time", time)
                data.put("userName", userName)
                data.put("day", day)
//            for (i in 0 until data.size) {
                progress?.dismiss()
                Log.e("DataFinal ", data.toString())

                add(
                    TextPrintable.Builder()
                        .setText(pName + "\n" + date + "\n" + dayOfTheWeek + ". " + time + "\n                   " + StringUtils.stripAccents(userName))
                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                        .setNewLinesAfter(1)
                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                        .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                        .build()
                )

                Picasso.get().load(EndPoint.ETE_IMAGE_BASEURL + barcode_img_path)
                    .into(object : Target {
                        override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                            var scaledBitmap: Bitmap? = scaleDown(bitmap, 320.0f, true)
                            var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                            add(ImagePrintable.Builder(bit).build())
                        }

                        override fun onBitmapFailed(
                            e: Exception,
                            errorDrawable: Drawable?
                        ) {
                        }

                        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
                    })
                add(
                    TextPrintable.Builder()
                        .setText("         " + "\n" + "       " + "\n")
                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                        .setNewLinesAfter(1)
                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                        .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                        .build()
                )
            }
            Log.e(TAG, "Printingg: " + printables)
            printing?.print(printables)

        } else {

            val printables = ArrayList<Printable>().apply {
                Log.e(TAG, "Printables: " + printing.toString())
                val data = HashMap<String, String>()
                data.put("Product", productName)
                data.put("Date", date)
                data.put("Time", time)
                data.put("userName", userName)
                data.put("day", day)
//            for (i in 0 until data.size) {
                progress?.dismiss()
                Log.e("DataFinal ", data.toString())

                add(
                    TextPrintable.Builder()
                        .setText(pName + "\n" + date + "\n" + dayOfTheWeek + ". " + time + "\n                   " +StringUtils.stripAccents(userName))
                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                        .setNewLinesAfter(1)
                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                        .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                        .build()
                )
            }
            Log.e(TAG, "Printingg: " + printables)
            printing?.print(printables)
        }
    }

    private fun printCustomLabels(msg: ArrayList<String>, images: String) {
        progress?.show()
        var imagee: String = images
        Log.e(TAG, "Printables: " + imagee)
        if (isLogoPrint == 0) {
            val printables = ArrayList<Printable>().apply {
                for (i in 0 until msg.size) {
                    progress?.dismiss()
                    Log.e("Printables:= ", msg[i])
                    add(
                        TextPrintable.Builder()
                            .setText(msg[i] + "\n")
                            .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                            .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                            .setFontSize(DefaultPrinter.FONT_SIZE_LARGE)
                            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                            .setNewLinesAfter(1)
                            .build()
                    )
                }
            }
            printing?.print(printables)
        } else {
            val printables = ArrayList<Printable>().apply {
                Log.e(TAG, "Printables: " + printing.toString())
                Picasso.get().load(images)
                    .into(object : Target {
                        override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
                            progress?.dismiss()
                            var scaledBitmap: Bitmap? = scaleDown(bitmap, 150.0f, true)
                            var bit: Bitmap = scaledBitmap?.let { toGrayscale(it) }!!
                            add(ImagePrintable.Builder(bit).build())
                            for (i in 0 until msg.size) {
                                progress?.dismiss()
                                add(
                                    TextPrintable.Builder()
                                        .setText(msg[i] + "\n")
                                        .setCharacterCode(DefaultPrinter.CHARCODE_WEU)
                                        .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                                        .setFontSize(DefaultPrinter.FONT_SIZE_LARGE)
                                        .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD)
                                        .setNewLinesAfter(1)
                                        .build()
                                )
                            }
                        }

                        override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {
                            progress?.dismiss()
                        }

                        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                            progress?.dismiss()
                        }
                    })
            }
            Log.e(TAG, "Printingg: " + printables)
            printing?.print(printables)
        }
    }

    fun scaleDown(
        realImage: Bitmap, maxImageSize: Float,
        filter: Boolean
    ): Bitmap? {
        val ratio = Math.min(
            maxImageSize / realImage.width,
            maxImageSize / realImage.height
        )
        val width = Math.round(ratio * realImage.width)
        val height = Math.round(ratio * realImage.height)
        return Bitmap.createScaledBitmap(
            realImage, width,
            height, filter
        )
    }

    fun toGrayscale(bmpOriginal: Bitmap): Bitmap {
        val width: Int
        val height: Int
        height = bmpOriginal.height
        width = bmpOriginal.width
        val bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
        val c = Canvas(bmpGrayscale)
        val paint = Paint()
        val cm = ColorMatrix()
        cm.setSaturation(0.0f)
        val f = ColorMatrixColorFilter(cm)
        paint.colorFilter = f
        c.drawBitmap(bmpOriginal, 0.0f, 0.0f, paint)
        return bmpGrayscale
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == ScanningActivity.SCANNING_FOR_PRINTER && resultCode == Activity.RESULT_OK)
            initViews()
    }

    fun getgogradetail() {
        loadershow()
        val string =  EndPoint.ETE_GET_GO_GRAB_PRODUCT_DETAILS
        println("$string.........................str")
        val stringRequest: StringRequest = object : StringRequest(Method.POST, string,
            Response.Listener { response ->
                try {
                    val jsonObject = JSONObject(response)
                    Log.e("response==", response)
                    vendor_logo = jsonObject.getString("vendor_logo")
                    val status = jsonObject.getInt("status")
                    val msg = jsonObject.getString("msg")
                    println("$status.........................str")
                    println("$msg.........................str")
                    if (status == 1) {
                        val jsonData = jsonObject.getJSONObject(Keyword.ETE_DATA)
                        note = jsonData.getString("note")
                        notesText?.setText(getString(R.string.percent_daily_value) + note + getString(
                            R.string.calorie_diet))
                        namee = jsonData.getString("name")
                        quantity = jsonData.getString("product_price")
                        enjoy_by = jsonData.getString("enjoy_by")
                        val productPrice = jsonData.getString("product_price")
                        barcode_img_path = jsonData.getString("barcode_img_path")
                        Ingredient = jsonData.getString("ingredient")
                        Allergens = jsonData.getString("allergens")
                        isLogoSelect = jsonData.get("print_logo") as Int;
                        isBarcodeSelect = jsonData.get("print_barcode") as Int;
                        val MainlabelArr = jsonData.getJSONArray("main_label")

                        if (isBarcodeSelect == 1) {

                            barcodeImage_GoGrab?.let {
                                Glide.with(applicationContext)
                                    .load(EndPoint.ETE_IMAGE_BASEURL + barcode_img_path)
                                    .into(it)
                            }
                        } else {
                            barcodeImage_GoGrab!!.visibility = View.GONE
                        }
                        val imgObj = jsonData.getJSONObject("image")
                        if (imgObj.has("image")) {

                            val imgg = imgObj.getString("image")
                            image = imgg
                            if (isLogoSelect == 1) {

                                productlogoimg?.let {
                                    Glide.with(applicationContext)
                                        .load(EndPoint.ETE_IMAGE_BASEURL+ vendor_logo?.substring(1))
                                        .into(it)
                                }
                            } else {
                                productlogoimg!!.visibility = View.GONE
                            }
                            println("LOGGER ETE_IMAGE_BASEURL + imgg: " + EndPoint.ETE_IMAGE_BASEURL + vendor_logo?.substring(1))
                        }
                        if (namee != null) {
                            nametxt?.setText(namee)
                        }
                        if (enjoy_by != null) {
                            enjoybytxt?.setText(enjoy_by)
                        }
                        val encodePrice:String?=decodeData(productPrice)
                        if (productPrice != null && productPrice != "null") {
                            if (!currentLanguage.equals("en")){
                                prodcutPrictxt?.setText(encodePrice)
                            }else{
                                prodcutPrictxt?.setText(productPrice)
                            }
                        }
                        if (Ingredient != null && Ingredient != "null") {
                            ingredientstxt?.setText(Ingredient)
                        }
                        if (Allergens != null) {
                            allergenstxt?.setText(Allergens)
                        }
                        for (i in 0 until MainlabelArr.length()) {
                            val jsonObject1 = MainlabelArr.getJSONObject(i)
                            val gson = Gson()
                            val goGrabLableFormetModel = gson.fromJson(
                                jsonObject1.toString(),
                                GoGrabLableFormetModel::class.java
                            )
                            goGrabLableFormetModels.add(goGrabLableFormetModel)
                            val sub_labels = jsonObject1.getJSONArray("sub_labels")
                            for (i in 0 until sub_labels.length()) {
                                val jsonObject = sub_labels.getJSONObject(i);
                                var parent_id: String = jsonObject.getString("parent_id")
                                var name: String = jsonObject.getString("name")
                                val option_value = jsonObject.getJSONObject("option_value")
                                var value: String = option_value.getString("value")
                                var status: Int = option_value.getInt("status")
                                if (status == 1) {
                                    val obj = JSONObject(value)
                                    val uDay = obj.getString("0").toString()
                                    val uHr = obj.getString("1").toString()
                                    val nameLeenth: String? = firstTen(name)
                                    Log.e("rgiorenbea; ", "" + nameLeenth)
                                    val uDayLeenth: String? = firstTwo(uDay)
                                    val decodeString:String?= uDayLeenth?.let { decodeData(it) }

                                    Log.e("rgiorenbea; ", "two: " + uDayLeenth)
                                    if (!currentLanguage.equals("en")){
                                        GranArray!!.add(
                                            nameLeenth + "      " + decodeString + "      " + uHr +
                                                    "%"
                                        )
                                    }else{
                                        GranArray!!.add(
                                            nameLeenth + "      " + uDay + "      " + uHr +
                                                    "%"
                                        )
                                    }

                                    if (!currentLanguage.equals("en")){
                                        list.add(PrintModel(nameLeenth, decodeString, uHr, parent_id))

                                        Log.e(
                                            TAG,
                                            "Printingg:lenght " + decodeString + "  uday=" + nameLeenth + "   persentage=" + uHr
                                        )
                                    }else{
                                        list.add(PrintModel(nameLeenth, uDayLeenth, uHr, parent_id))

                                        Log.e(
                                            TAG,
                                            "Printingg:lenght " + uDayLeenth + "  uday=" + nameLeenth + "   persentage=" + uHr
                                        )
                                    }

                                    //    printing?.print(printables)
                                }
                            }
                        }
                        goGrabLableFormetAdapter = GoGrabLableFormetAdapter(
                            this,
                            goGrabLableFormetModels,
                            passArrayList
                        )

                        recyclerView!!.setHasFixedSize(true)
                        val linearLayoutManager = LinearLayoutManager(
                            applicationContext,
                            LinearLayoutManager.VERTICAL,
                            false
                        )
                        recyclerView!!.layoutManager = linearLayoutManager
                        recyclerView!!.setAdapter(goGrabLableFormetAdapter)
                    } else {
                        if (msg.equals(getString(R.string.token_expired))) {
                            Tools.clearData(this)
                        }
                        Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
                    }
                } catch (json: JSONException) {
                    Log.e("fbkewbfvkew: ", json.message.toString())
                    json.printStackTrace()
                }
                loaderHide()
            }, Response.ErrorListener { error ->
                if (error is TimeoutError || error is NoConnectionError) {
                    Toast.makeText(this, getString(R.string.internet_connection_fail), Toast.LENGTH_SHORT)
                        .show()
                }
                loaderHide()
            }) {
            @Throws(AuthFailureError::class)
            override fun getHeaders(): Map<String, String> {
                val params: MutableMap<String, String> = HashMap()
                params["Authorization"] = "Bearer " + loginToken2
                Log.e("sdnkefw: ", "" + loginToken2)
                return params
            }

            @Throws(AuthFailureError::class)
            override fun getParams(): Map<String, String> {
                val map: MutableMap<String, String> = HashMap()
                map["outlet_id[]"] = oID!!
                map["product_id"] = pID!!
                map["language"] = currentLanguage!!

                println("$map...............mapapapap")
                return map
            }

        }
        stringRequest.retryPolicy = DefaultRetryPolicy(
            Keyword.MY_SOCKET_TIMEOUT,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
        )
        val requestQueue = Volley.newRequestQueue(applicationContext)
        requestQueue.add(stringRequest)
        requestQueue.addRequestFinishedListener<Any> { requestQueue.cache.clear() }
    }

    private fun decodeData(value: String): String {
        return URLDecoder.decode(value)

    }

    fun firstTen(str: String): String? {
        return if (str.length <= 15) str else str.substring(0, 15)
    }

    fun firstTwo(str: String): String? {
        return if (str.length < 5) str else str.substring(0, 5)
    }
}