package chaoran.business.engine;

/*
 **********************************************
 *      DATE           PERSON       REASON
 *    2021-02-19          FXY        Created
 **********************************************
 */


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;
import chaoran.business.engine.entity.BlueToothSetting;
import chaoran.business.engine.entity.Setting;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

public abstract class BlueToothWrapperEngine implements PrintEngine {

    //打印驱动标志
    protected static final int PRINT_TYPE = 1664;

    //蓝牙标识
    private UUID uuid;

    //上下文
    private Context context;

    //蓝牙适配器
    protected BluetoothAdapter bluetoothAdapter;
    //蓝牙socket对象
    private BluetoothSocket bluetoothSocket;

    //开启蓝牙且连接设备
    public abstract void startThenConnect();

    //关闭蓝牙
    public abstract void close();

    //获取绑定的设备
    public abstract BluetoothDevice getBoundDevice();

    public BlueToothWrapperEngine(Context context) {
        this.context = context;
        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        this.uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    }

    //蓝牙打印机打印操作
    @Override
    public void print(File file) {
        BluetoothDevice boundDevice = getBoundDevice();
        //先获取连接上的设备继续操作
        if (null == boundDevice) {
            Toast.makeText(context, "蓝牙未连接设备或者连接的设备不是打印机", Toast.LENGTH_LONG).show();
            return;
        }
        new PrintThread(boundDevice, file).start();
    }

    @Override
    public Setting getSetting() {
        SharedPreferences sharedPreferences = context.getSharedPreferences("BlueToothEngine", Context.MODE_PRIVATE);
        String name = sharedPreferences.getString("name", "");
        String address = sharedPreferences.getString("address", "");
        return new BlueToothSetting(name, address);
    }

    @Override
    public boolean saveSetting(Setting s) {
        BlueToothSetting blueToothSetting = (BlueToothSetting) s;
        SharedPreferences sharedPreferences = context.getSharedPreferences("BlueToothEngine", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("name", blueToothSetting.getName());
        edit.putString("address", blueToothSetting.getAddress());
        edit.commit();
        return true;
    }


    /**
     * 连接为客户端
     */
    private class PrintThread extends Thread {

        private File file;

        public PrintThread(BluetoothDevice device, File file) {
            this.file = file;
            try {
                bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void run() {
            //取消的发现,因为它将减缓连接
            bluetoothAdapter.cancelDiscovery();
            OutputStream outputStream = null;
            FileInputStream fileInputStream = null;
            try {
                //连接socket
                bluetoothSocket.connect();
                //连接成功获取输出流
                outputStream = bluetoothSocket.getOutputStream();
                fileInputStream = new FileInputStream(file);
                byte[] data = new byte[1024];
                int number;
                while ((number = fileInputStream.read(data)) != -1) {
                    outputStream.write(data, 0, number);
                    outputStream.flush();
                }
                fileInputStream.close();
                outputStream.close();
            } catch (Exception connectException) {
                try {
                    fileInputStream.close();
                    outputStream.close();
                    bluetoothSocket.close();
                } catch (Exception closeException) {
                    closeException.printStackTrace();
                }
            }
        }
    }
}
