package com.kozen.printerhelper.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
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;

import java.io.IOException;

public class TextActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    private TextView mTextViewTextCharacter, mTextViewTextSize, mTextViewTypeface, mTextViewAlign;
    private CheckBox mCheckBox1, mCheckBox2;
    private EditText mEditText;
    private LinearLayout mLayout, mLinearLayout;
    private int recordCharacter = 17;
    private boolean isBold = false;
    private boolean isUnderLine = false;

    private String[] mStrings = new String[]{"CP437", "CP850", "CP860", "CP863", "CP865", "CP857", "CP737", "CP928", "Windows-1252", "CP866", "CP852", "CP858", "CP874", "Windows-775", "CP855", "CP862", "CP864", "GB18030", "BIG5", "KSC5601", "utf-8"};

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

        mTextViewTextCharacter = findViewById(R.id.text_text_character);
        mTextViewTextSize = findViewById(R.id.text_text_size);
        mTextViewTypeface = findViewById(R.id.text_text_typeface);
        mTextViewAlign = findViewById(R.id.text_text_align);
        mCheckBox1 = findViewById(R.id.text_bold);
        mCheckBox2 = findViewById(R.id.text_underline);
        mEditText = findViewById(R.id.text_text);

        mLinearLayout = findViewById(R.id.text_all);
        mLayout = findViewById(R.id.text_set);

        mLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                mLinearLayout.getWindowVisibleDisplayFrame(r);
                if(r.bottom < 800){
                    mLayout.setVisibility(View.GONE);
                }else{
                    mLayout.setVisibility(View.VISIBLE);
                }
            }
        });

        mCheckBox1.setOnCheckedChangeListener(this);
        mCheckBox2.setOnCheckedChangeListener(this);


//        findViewById(R.id.text_typeface).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                final String[] list = new String[]{"NORMAL", "BOLD", "SERIF", "Arial", "FangHei", "FranklinGothic", "Haettenschweiler", "HuaSong", "SansMono"};
//                final ListDialog listDialog = DialogCreator.createListDialog(TextActivity.this, getResources().getString(R.string.type_text), getResources().getString(R.string.cancel), list);
//                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
//                    @Override
//                    public void OnItemClick(int position) {
//                        mTextViewTypeface.setText(list[position]);
//                        recordTypeface = position;
//                        listDialog.cancel();
//                    }
//                });
//                listDialog.show();
//            }
//        });

        findViewById(R.id.text_character).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ListDialog listDialog = DialogCreator.createListDialog(TextActivity.this, getResources().getString(R.string.characterset), getResources().getString(R.string.cancel), mStrings);
                listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        mTextViewTextCharacter.setText(mStrings[position]);
                        recordCharacter = position;
                        listDialog.cancel();
                    }
                });
                listDialog.show();
            }
        });

//        findViewById(R.id.text_size).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showSeekBarDialog(TextActivity.this, getResources().getString(R.string.size_text), 12, 48, mTextViewTextSize);
//            }
//        });

//        findViewById(R.id.text_align).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                final String[] list = getResources().getStringArray(R.array.align);
//                final ListDialog listDialog = DialogCreator.createListDialog(TextActivity.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]);
//                        recordAlign = position;
//                        listDialog.cancel();
//                    }
//                });
//                listDialog.show();
//            }
//        });
    }

    public void onClick(View view) {
        String content = mEditText.getText().toString();
        int size = Integer.parseInt(mTextViewTextSize.getText().toString());

        if (!BluetoothUtil.isBlueToothPrinter) {
            KozenPrintHelper.getInstance().printText(content, size, isBold, isUnderLine);
            KozenPrintHelper.getInstance().feedPaper();
        } else {
            printByBluTooth(content);
        }
    }

    private void printByBluTooth(String content) {
        try {
            if (isBold) {
                BluetoothUtil.sendData(ESCUtil.boldOn());
            } else {
                BluetoothUtil.sendData(ESCUtil.boldOff());
            }

            if (isUnderLine) {
                BluetoothUtil.sendData(ESCUtil.underlineWithOneDotWidthOn());
            } else {
                BluetoothUtil.sendData(ESCUtil.underlineOff());
            }

            if (recordCharacter < 17) {
                BluetoothUtil.sendData(ESCUtil.singleByte());
                BluetoothUtil.sendData(ESCUtil.setCodeSystemSingle(codeParse(recordCharacter)));
            } else {
                BluetoothUtil.sendData(ESCUtil.singleByteOff());
                BluetoothUtil.sendData(ESCUtil.setCodeSystem(codeParse(recordCharacter)));
            }

            KozenPrintHelper.getInstance().setPrintEncode(mStrings[recordCharacter]);
            BluetoothUtil.sendData(content.getBytes(mStrings[recordCharacter]));
            BluetoothUtil.sendData(ESCUtil.nextLine(3));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private byte codeParse(int value) {
        byte res = 0x00;
        switch (value) {
            case 0:
                res = 0x00;
                break;
            case 1:
            case 2:
            case 3:
            case 4:
                res = (byte) (value + 1);
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
                res = (byte) (value + 8);
                break;
            case 12:
                res = 21;
                break;
            case 13:
                res = 33;
                break;
            case 14:
                res = 34;
                break;
            case 15:
                res = 36;
                break;
            case 16:
                res = 37;
                break;
            case 17:
            case 18:
            case 19:
                res = (byte) (value - 17);
                break;
            case 20:
                res = (byte) 0xff;
                break;
            default:
                break;
        }
        return (byte) res;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.text_bold:
                isBold = isChecked;
                break;
            case R.id.text_underline:
                isUnderLine = isChecked;
                break;
            default:
                break;
        }
    }
}
