package ai.pai.lensman.printer;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.List;

import ai.pai.lensman.R;
import ai.pai.lensman.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class PrinterSettingActivity extends BaseActivity implements PrinterSettingContract.View,AdapterView.OnItemClickListener {

    @BindView(R.id.listview_bt_devices)    ListView btDevicesList;
    @BindView(R.id.tv_printer_status)  TextView printerStatusText;
    @BindView(R.id.tv_bluetooth_status) TextView bluetoothStatusText;
    @BindView(R.id.tb_bluetooth_switch)   ToggleButton btSwitchToggle;

    private BluetoothDeviceListAdapter deviceAdapter;
    private PrinterSettingContract.Presenter presenter;

    public static final int    REQUEST_ENABLE_BT      = 2;
    public static final int    REQUEST_CONNECT_DEVICE = 3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer_setting);
        unbinder = ButterKnife.bind(this);

        deviceAdapter = new BluetoothDeviceListAdapter(this);
        btDevicesList.setAdapter(deviceAdapter);
        btDevicesList.setOnItemClickListener(this);
        presenter = new PrinterSettingPresenter(this,this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.start();
    }

    @Override
    protected void onDestroy() {
        presenter.stop();
        super.onDestroy();
    }

    @Override
    public void onBluetoothEnabled() {
        btSwitchToggle.setChecked(true);
        bluetoothStatusText.setText(R.string.bt_is_enabled);
    }

    @Override
    public void onBluetoothDisabled() {
        btSwitchToggle.setChecked(false);
        bluetoothStatusText.setText(R.string.bt_is_disabled);
    }

    @Override
    public void onPrinterOk() {
        printerStatusText.setText(R.string.printer_is_ok);
    }

    @Override
    public void onPrinterError(String error) {
        printerStatusText.setText(error);
    }

    @Override
    public void onPrinterConnected() {
        showToast(getString(R.string.printer_is_connected));
    }

    @Override
    public void onPrinterDisconnected() {
        showToast(getString(R.string.printer_is_disconnected));
    }

    @Override
    public void onNewDeviceDiscovered(BluetoothDevice device) {
        deviceAdapter.addDevice(device);
    }


    @Override
    public void onPrinterTestPageFail() {

    }

    @Override
    public void onPairedDeviceDiscovered(List<BluetoothDevice> devices) {
        if(devices==null|| devices.size()==0){
            return;
        }
        deviceAdapter.setDeviceList((ArrayList<BluetoothDevice>)devices);
    }


    @Override
    public void showToast(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.tv_printer_status)
    void checkStatus(){
        presenter.queryPrinterStatus();
    }

    @OnClick(R.id.tv_print_test)
    void testPrint(){
        presenter.printTestPage();
    }

    @OnClick(R.id.title_bar_back_layout)
    void back(){
        finish();
    }

    @OnCheckedChanged(R.id.tb_bluetooth_switch)
    void switchBluetooth(){
        if(!presenter.queryBluetoothStatus()){
            Intent enableIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent,
                    REQUEST_ENABLE_BT);
        }else{
            BluetoothAdapter.getDefaultAdapter().disable();
            onBluetoothDisabled();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK){
            if(requestCode == REQUEST_ENABLE_BT){
                onBluetoothEnabled();
                onPairedDeviceDiscovered(presenter.queryPairedDevices());
                presenter.discoverNewDevices();
                presenter.queryPrinterStatus();
            }else if(requestCode == REQUEST_CONNECT_DEVICE){

            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        presenter.connectPrinter(deviceAdapter.getDeviceList().get(i));
    }
}
