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.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.android.common.utils.DeviceUtils;

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

import ai.pai.lensman.R;
import ai.pai.lensman.base.BaseActivity;
import ai.pai.lensman.qrcode.QRCreateUtils;
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;
    @BindView(R.id.iv_qrcode)   ImageView qrCodeImg;

    private BluetoothDeviceListAdapter deviceAdapter;
    private PrinterSettingContract.Presenter presenter;

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

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

        qrcode = getIntent().getStringExtra("qrcode");
        if(TextUtils.isEmpty(qrcode)){
            finish();
            return;
        }
        qrCodeImg.post(new Runnable() {
            @Override
            public void run() {
                try{
                    qrCodeImg.setImageBitmap(QRCreateUtils.Create2DCode(qrcode, DeviceUtils.dip2px(PrinterSettingActivity.this,240)));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        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 onPrinterStatusFetched(String msg) {
        printerStatusText.setText(msg);
    }


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


    @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(){
        showToast(getString(R.string.query_processing));
        presenter.queryPrinterStatus();
    }

    @OnClick(R.id.btn_print_qr)
    void printerQR(){
        presenter.printQR(qrcode);
    }

    @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));
    }
}
