package com.kozen.printerhelper.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

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

public abstract class BaseActivity extends AppCompatActivity {

    Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler();
        initPrinterStyle();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(handler != null){
            handler.removeCallbacksAndMessages(null);
        }
    }

    /**
     *  Initialize the printer
     *  All style settings will be restored to default
     */
    private void initPrinterStyle() {
        KozenPrintHelper.checkIPrinterService(KozenPrintHelper.mIPrinterService);
        KozenPrintHelper.getInstance().setPrintEncode("GBK");
        if(BluetoothUtil.isBlueToothPrinter){
            BluetoothUtil.sendData(ESCUtil.init_printer());
        }else{
            KozenPrintHelper.getInstance().initPrinter();
        }
    }

    /**
     * set title
     * @param title title name
     */
    void setMyTitle(String title){
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setTitle(title);
        }
    }

    /**
     * set title
     * @param title title res
     */
    void setMyTitle(@StringRes int title){
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setTitle(title);
        }
        setSubTitle();
    }

    /**
     * set sub title
     */
    void setSubTitle(){
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            if(BluetoothUtil.isBlueToothPrinter){
                actionBar.setSubtitle("bluetooth®");
            }else{
                if(KozenPrintHelper.getInstance().kozenPrinter == KozenPrintHelper.NoKozenPrinter){
                    actionBar.setSubtitle("no printer");
                }else if(KozenPrintHelper.getInstance().kozenPrinter == KozenPrintHelper.CheckKozenPrinter){
                    actionBar.setSubtitle("connecting");
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setSubTitle();
                        }
                    }, 2000);
                }else if(KozenPrintHelper.getInstance().kozenPrinter == KozenPrintHelper.FoundKozenPrinter){
                    actionBar.setSubtitle("");
                }else{
                    KozenPrintHelper.getInstance().initIPrinterService(this);
                }
            }
        }
    }

    /**
     * set back
     */
    void setBack(){
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.back);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.hexprint, menu);
        return true;
    }

    EditTextDialog mEditTextDialog;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.action_print:
                mEditTextDialog = DialogCreator.createEditTextDialog(this,
                    getResources().getString(R.string.cancel), getResources().getString(R.string.confirm), getResources().getString(R.string.input_order), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditTextDialog.cancel();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String text = mEditTextDialog.getEditText().getText().toString();
                        byte[] data = BytesUtil.getBytesFromHexString(text);
                        if(BluetoothUtil.isBlueToothPrinter){
                            BluetoothUtil.sendData(data);
                        }else{
                            KozenPrintHelper.getInstance().sendRawData(data);
                        }
                        mEditTextDialog.cancel();
                    }
                }, null);
                mEditTextDialog.show();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    /**
     * custom seekbar dialog
     */
    void showSeekBarDialog(Context context, String title, final int min, final int max, final TextView set) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.widget_seekbar, null);
        builder.setView(view);
        builder.setCancelable(false);
        final AlertDialog dialog = builder.create();
        TextView tv_title = (TextView) view.findViewById(R.id.sb_title);
        TextView tv_start = (TextView) view.findViewById(R.id.sb_start);
        TextView tv_end = (TextView) view.findViewById(R.id.sb_end);
        final TextView tv_result = (TextView) view.findViewById(R.id.sb_result);
        TextView tv_ok = (TextView) view.findViewById(R.id.sb_ok);
        TextView tv_cancel = (TextView) view.findViewById(R.id.sb_cancel);
        SeekBar sb = (SeekBar) view.findViewById(R.id.sb_seekbar);
        tv_title.setText(title);
        tv_start.setText(min + "");
        tv_end.setText(max + "");
        tv_result.setText(set.getText());
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                set.setText(tv_result.getText());
                dialog.cancel();
            }
        });
        sb.setMax(max - min);
        sb.setProgress(Integer.parseInt(set.getText().toString()) - min);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int rs = min + progress;
                tv_result.setText(rs + "");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        dialog.show();
    }
}
