package com.kozen.printerhelper.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

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

public class BitmapActivity extends BaseActivity {
    private ImageView mImageView;
    private TextView mTextViewAlign, mTextViewType, mTextViewOrientation;
    private LinearLayout llStyle, llType, llOrientation;
    private Bitmap bitmap, bitmap1;
    private CheckBox mCheckBoxWidth, mCheckBoxHeight;

    private int mytype;
    private int myorientation = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap);
        setMyTitle(R.string.bitmap_title);
        setBack();
        initView();
        if(!BluetoothUtil.isBlueToothPrinter){
            llStyle.setVisibility(View.GONE);
            llType.setVisibility(View.GONE);
            llOrientation.setVisibility(View.VISIBLE);
        }else{
            llStyle.setVisibility(View.VISIBLE);
            llType.setVisibility(View.VISIBLE);
            llOrientation.setVisibility(View.GONE);
        }
    }

    private void initView() {
        mTextViewAlign = findViewById(R.id.pic_align_info);
        mTextViewType = findViewById(R.id.pic_type_info);
        mTextViewOrientation = findViewById(R.id.pic_orientation_info);
        mCheckBoxWidth = findViewById(R.id.pic_width);
        mCheckBoxHeight = findViewById(R.id.pic_height);
        llStyle = findViewById(R.id.pic_style);
        llType = findViewById(R.id.pic_type);
        llOrientation = findViewById(R.id.pic_orientation);
        findViewById(R.id.pic_align).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                final String[] list = getResources().getStringArray(R.array.align);
                final ListDialog listDialog =  DialogCreator.createListDialog(BitmapActivity.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();
            }
        });

//        llOrientation.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                final String[] orientation = new String[]{getResources().getString(R.string.pic_hor),getResources().getString(R.string.pic_ver)};
//                final ListDialog listDialog = DialogCreator.createListDialog(BitmapActivity.this, getResources().getString(R.string.pic_pos), getResources().getString(R.string.cancel), orientation);
//                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
//                    @Override
//                    public void OnItemClick(int position) {
//                        mTextViewOrientation.setText(orientation[position]);
//                        myorientation = position;
//                        listDialog.cancel();
//                    }
//                });
//                listDialog.show();
//            }
//        });

        llType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] type = new String[]{getResources().getString(R.string.pic_mode1),getResources().getString(R.string.pic_mode2), getResources().getString(R.string.pic_mode3), getResources().getString(R.string.pic_mode4), getResources().getString(R.string.pic_mode5)};
                final ListDialog listDialog = DialogCreator.createListDialog(BitmapActivity.this, getResources().getString(R.string.pic_mode), getResources().getString(R.string.cancel), type);
                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        mTextViewType.setText(type[position]);
                        mytype = position;
                        if(position == 0){
                            llStyle.setVisibility(View.VISIBLE);
                        }else{
                            llStyle.setVisibility(View.INVISIBLE);
                        }
                        listDialog.cancel();
                    }
                });
                listDialog.show();
            }
        });
        mImageView = findViewById(R.id.bitmap_imageview);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTargetDensity = 160;
        options.inDensity = 160;
        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);

        }

        if (bitmap1 == null) {
            bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.test1, options);
            bitmap1 = scaleImage(bitmap1);
        }
        if (BluetoothUtil.isBlueToothPrinter) {
            mImageView.setImageDrawable(new BitmapDrawable(bitmap1));
        } else {
            mImageView.setImageDrawable(new BitmapDrawable(bitmap));
        }

    }

    /**
     *  Scaled image width is an integer multiple of 8 and can be ignored
     */
    private Bitmap scaleImage(Bitmap bitmap1) {
        int width = bitmap1.getWidth();
        int height = bitmap1.getHeight();
        // 设置想要的大小
        int newWidth = (width/8+1)*8;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, 1);
        // 得到新的图片
        return Bitmap.createBitmap(bitmap1, 0, 0, width, height, matrix, true);
    }

    public void onClick(View view) {
        if (!BluetoothUtil.isBlueToothPrinter) {
            KozenPrintHelper.getInstance().printBitmap(bitmap, myorientation);
            KozenPrintHelper.getInstance().feedPaper();
        } else {
            if(mytype == 0){
                if(mCheckBoxWidth.isChecked() && mCheckBoxHeight.isChecked()){
                    BluetoothUtil.sendData(ESCUtil.printBitmap(bitmap1, 3));
                }else if(mCheckBoxWidth.isChecked()){
                    BluetoothUtil.sendData(ESCUtil.printBitmap(bitmap1, 1));
                }else if(mCheckBoxHeight.isChecked()){
                    BluetoothUtil.sendData(ESCUtil.printBitmap(bitmap1, 2));
                }else{
                    BluetoothUtil.sendData(ESCUtil.printBitmap(bitmap1, 0));
                }
            }else if(mytype == 1){
                BluetoothUtil.sendData(ESCUtil.selectBitmap(bitmap1, 0));
            }else if(mytype == 2){
                BluetoothUtil.sendData(ESCUtil.selectBitmap(bitmap1, 1));
            }else if(mytype == 3){
                BluetoothUtil.sendData(ESCUtil.selectBitmap(bitmap1, 32));
            }else if(mytype == 4){
                BluetoothUtil.sendData(ESCUtil.selectBitmap(bitmap1, 33));
            }
            BluetoothUtil.sendData(ESCUtil.nextLine(3));
        }
    }
}
