package com.kozen.printerhelper.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.kozen.kozenui.dialog.DialogCreator;
import com.kozen.kozenui.dialog.EditTextDialog;
import com.kozen.kozenui.dialog.ListDialog;
import com.kozen.printerhelper.R;
import com.kozen.printerhelper.helper.KozenPrintHelper;
import com.kozen.printerhelper.utils.BitmapUtil;
import com.kozen.printerhelper.utils.BluetoothUtil;
import com.kozen.printerhelper.utils.ESCUtil;

public class BarCodeActivity extends BaseActivity {
    private TextView mTextViewContent, mTextViewEncode, mTextViewPosition, mTextViewWidth, mTextViewHeight, mTextViewAlign;
    private ImageView mImageView;
    private int encode = 8;
    private int position = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcode);
        setMyTitle(R.string.barcode_title);
        setBack();

        mTextViewContent = findViewById(R.id.bc_text_content);
        mTextViewEncode = findViewById(R.id.bc_text_encode);
        mTextViewPosition = findViewById(R.id.bc_text_position);
        mTextViewWidth = findViewById(R.id.bc_text_width);
        mTextViewHeight = findViewById(R.id.bc_text_height);
        mTextViewAlign = findViewById(R.id.bc_text_align);
        mImageView = findViewById(R.id.bc_image);

        findViewById(R.id.bc_content).setOnClickListener(new View.OnClickListener() {
            EditTextDialog mDialog;

            @Override
            public void onClick(View v) {
                mDialog = DialogCreator.createEditTextDialog(BarCodeActivity.this,getResources().getString(R.string.cancel), getResources().getString(R.string.confirm), getResources().getString(R.string.input_barcode), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.cancel();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mTextViewContent.setText(mDialog.getEditText().getText());
                        mDialog.cancel();
                    }
                }, null);
                mDialog.setHintText(String.valueOf(mTextViewContent.getText()));
                mDialog.show();
            }
        });

        findViewById(R.id.bc_encode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] list = new String[]{"UPC-A", "UPC-E", "EAN13", "EAN8", "CODE39", "ITF", "CODABAR", "CODE93", "CODE128A", "CODE128B", "CODE128C"};
                final ListDialog listDialog = DialogCreator.createListDialog(BarCodeActivity.this, getResources().getString(R.string.encode_barcode), getResources().getString(R.string.cancel), list);
                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        mTextViewEncode.setText(list[position]);
                        encode = position;
                        listDialog.cancel();
                    }
                });
                listDialog.show();
            }
        });

        findViewById(R.id.bc_position).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] list = getResources().getStringArray(R.array.textPosition);
                final ListDialog listDialog = DialogCreator.createListDialog(BarCodeActivity.this, getResources().getString(R.string.text_position), getResources().getString(R.string.cancel), list);
                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        mTextViewPosition.setText(list[position]);
                        BarCodeActivity.this.position = position;
                        listDialog.cancel();
                    }
                });
                listDialog.show();
            }
        });

        findViewById(R.id.bc_width).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSeekBarDialog(BarCodeActivity.this, getResources().getString(R.string.barcode_width), 2, 6, mTextViewWidth);
            }
        });

        findViewById(R.id.bc_height).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSeekBarDialog(BarCodeActivity.this, getResources().getString(R.string.barcode_height), 1, 255, mTextViewHeight);
            }
        });

//        findViewById(R.id.bc_align).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                final String[] list = getResources().getStringArray(R.array.align);
//                final ListDialog listDialog = DialogCreator.createListDialog(BarCodeActivity.this, getResources().getString(R.string.align_form), getResources().getString(R.string.cancel), list);
//                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
//                    @Override
//                    public void OnItemClick(int position) {
//                        mTextViewAlign.setText(list[position]);
//                        listDialog.cancel();
//                    }
//                });
//                listDialog.show();
//            }
//        });
    }

    public void onClick(View view) {
        String text = mTextViewContent.getText().toString();
        int symbology;
        if(encode > 7){
            symbology = 8;
        }else{
            symbology = encode;
        }
        Bitmap bitmap = BitmapUtil.generateBitmap(text, symbology, 700, 400);
        if (bitmap != null) {
            mImageView.setImageDrawable(new BitmapDrawable(bitmap));
        }else{
            Toast.makeText(this, R.string.toast_9, Toast.LENGTH_LONG).show();
        }

        int height = Integer.parseInt(mTextViewHeight.getText().toString());
        int width = Integer.parseInt(mTextViewWidth.getText().toString());
        if (!BluetoothUtil.isBlueToothPrinter) {
            KozenPrintHelper.getInstance().printBarCode(text, width, height, position, encode);
            KozenPrintHelper.getInstance().feedPaper();
        } else {
            BluetoothUtil.sendData(ESCUtil.getPrintBarCode(text, encode, height, width, position));
            BluetoothUtil.sendData(ESCUtil.nextLine(3));
        }
    }
}
