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 QrActivity extends BaseActivity {
    private ImageView mImageView;
    private TextView mTextViewContent, mTextViewNum, mTextViewSize, mTextViewEl, mTextViewAlign;
    private int print_num = 0;
    private int error_level = 3;

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

        mImageView = findViewById(R.id.qr_image);
        mTextViewContent = findViewById(R.id.qr_text_content);
        mTextViewNum = findViewById(R.id.qr_text_num);
        mTextViewSize = findViewById(R.id.qr_text_size);
        mTextViewEl = findViewById(R.id.qr_text_el);
        mTextViewAlign = findViewById(R.id.qr_align_info);

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

            @Override
            public void onClick(View v) {
                mDialog = DialogCreator.createEditTextDialog(QrActivity.this, getResources().getString(R.string.cancel), getResources().getString(R.string.confirm), getResources().getString(R.string.input_qrcode), 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.qr_num).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] mStrings = new String[]{getResources().getString(R.string.single), getResources().getString(R.string.twice)};
                final ListDialog listDialog = DialogCreator.createListDialog(QrActivity.this,  getResources().getString(R.string.array_qrcode),getResources().getString(R.string.cancel), mStrings);
                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        if (!BluetoothUtil.isBlueToothPrinter) {
                            Toast.makeText(QrActivity.this, R.string.toast_7, Toast.LENGTH_LONG).show();
                            position = 0;
                        } else {
                            mTextViewSize.setText("3");
                        }
                        mTextViewNum.setText(mStrings[position]);
                        print_num = position;
                        listDialog.cancel();
                    }
                });
                listDialog.show();
            }
        });

        findViewById(R.id.qr_size).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSeekBarDialog(QrActivity.this, getResources().getString(R.string.size_qrcode), 1, 4, mTextViewSize);
            }
        });

        findViewById(R.id.qr_el).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] el = getResources().getStringArray(R.array.errorLevel);
                final ListDialog listDialog = DialogCreator.createListDialog(QrActivity.this, getResources().getString(R.string.error_qrcode), getResources().getString(R.string.cancel), el);
                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        mTextViewEl.setText(el[position]);
                        error_level = position;
                        listDialog.cancel();
                    }
                });
                listDialog.show();
            }
        });

        findViewById(R.id.qr_align).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                final String[] list = getResources().getStringArray(R.array.align);
                final ListDialog listDialog =  DialogCreator.createListDialog(QrActivity.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]);
                        if(!BluetoothUtil.isBlueToothPrinter){
                            KozenPrintHelper.getInstance().setAlign(position);
                        }else{
                            byte[] send;
                            if(position == 0){
                                send = ESCUtil.alignLeft();
                            }else if(position == 1){
                                send = ESCUtil.alignCenter();
                            }else {
                                send = ESCUtil.alignRight();
                            }
                            BluetoothUtil.sendData(send);
                        }
                        listDialog.cancel();
                    }
                });
                listDialog.show();
            }
        });
    }

    public void onClick(View view) {
        String text = mTextViewContent.getText().toString();
        int size = Integer.parseInt(mTextViewSize.getText().toString());
        Bitmap bitmap = BitmapUtil.generateBitmap(text, 9, 700, 700);
        if (bitmap != null) {
            mImageView.setImageDrawable(new BitmapDrawable(bitmap));
        }

        if (!BluetoothUtil.isBlueToothPrinter) {
            KozenPrintHelper.getInstance().printQr(text, size, error_level);
            KozenPrintHelper.getInstance().feedPaper();
        } else {
            if (print_num == 0) {
                BluetoothUtil.sendData(ESCUtil.getPrintQRCode(mTextViewContent.getText().toString(), size, error_level));
                BluetoothUtil.sendData(ESCUtil.nextLine(3));
            } else {
                BluetoothUtil.sendData(ESCUtil.getPrintDoubleQRCode(mTextViewContent.getText().toString(), mTextViewContent.getText().toString(), size, error_level));
                BluetoothUtil.sendData(ESCUtil.nextLine(3));
            }

        }
    }
}
