package com.creyond.ecgdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;

import com.creyond.sdk.JsonUtil;
import com.creyond.support.bluetooth.Bluetooth;
import com.creyond.support.bluetooth.Device;
import com.creyond.support.bluetooth.ScanListener;
import com.gengqiquan.adapter.adapter.SBAdapter;
import com.gengqiquan.adapter.interfaces.Holder;


public class DeviceListActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        setTitle("搜索设备");
        final SBAdapter sbAdapter = new SBAdapter<Device>(this)
                .layout(R.layout.device_list_item)
                .bindViewData(new com.gengqiquan.adapter.interfaces.Converter<Device>() {
                    @Override
                    public void convert(Holder holder, final Device item) {
                        holder.setText(R.id.tv_name, item.getName() + ":");
                        holder.setText(R.id.tv_number, item.getID());
                        holder.setText(R.id.tv_mac, item.getMac());
                        holder.getItemView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                setResult(RESULT_OK, new Intent().putExtra("device", JsonUtil.toJson(item)));
                                finish();
                            }
                        });
                    }
                });
        ListView lv_device = findViewById(R.id.lv_device);
        lv_device.setAdapter(sbAdapter);
        Bluetooth.scan(new ScanListener() {
            @Override
            public void findOne(Device device) {
                findViewById(R.id.loading).setVisibility(View.GONE);
                sbAdapter.getList().add(device);
                sbAdapter.notifyDataChanged();
            }

            @Override
            public void startScan() {
            }

            @Override
            public void scanFail(String msg) {
                error("手机蓝牙设备不可用");
            }

            @Override
            public void scanFinished() {
            }

        });


    }

}
