import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:vnote_device_plugin/models/device.dart';
import 'package:vnote_device_plugin/vnote_device_plugin.dart';
import 'package:vnote_device_plugin_example/search.dart';
import 'package:vnote_device_plugin_example/sp_o2.dart';
import 'package:vnote_device_plugin_example/sugar.dart';
import 'package:vnote_device_plugin_example/twelve_heart.dart';
import 'package:vnote_device_plugin_example/urine.dart';
import 'package:vnote_device_plugin_example/weight_height.dart';
import 'package:vnote_device_plugin_example/widgets/demo.dart';

import 'device.dart';
import 'global.dart';
import 'heart.dart';
import 'ic_reader.dart';
import 'nibp.dart';
import 'temp.dart';
import 'weight.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  DeviceInfo? _deviceInfo;
  final _vnoteDevicePlugin = VnoteDevicePlugin();
  static const List<String> _typeValues = [
    "temp",
    "weight",
    "sugar",
    "spo2",
    "nibp",
    "heart",
    "urine",
    "ic_reader",
    "twelveheart",
    "weight_height",
  ];
  static const List<String> _typeNames = [
    "体温",
    "体重",
    "血糖",
    "血氧",
    "血压",
    "心电",
    "尿液",
    "人证",
    "12导心电",
    "身高体重",
  ];
  int _typeIndex = 0;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await _vnoteDevicePlugin.getPlatformVersion() ??
          'Unknown platform version';
      await Global.init();
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          actions: [
            IconButton(
              onPressed: () async {
                // VnoteDevicePluginPlatform.instance.callAction(
                //   "SEARCH_STOP",
                //   {
                //     "TYPE": "temp",
                //   },
                // );
                Get.put<DevicesSettingController>(DevicesSettingController());
                await Get.to(() => const DevicesSettingPage());
                Get.delete<DevicesSettingController>();
              },
              icon: const Icon(Icons.stop),
            ),
          ],
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // Text('Running on: $_platformVersion\n'),
              _buildTypeSelect(),
              const SizedBox(height: 8),
              DeviceCard(data: _deviceInfo),
              if (_deviceInfo != null) ...[
                const SizedBox(height: 8),
                _buildExamCard(),
              ],
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            final result = await SearchDialog.dialog(_typeValues[_typeIndex]);
            if (result != null) {
              setState(() {
                _deviceInfo = result;
              });
            } else {
              //
            }
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              Icon(Icons.search),
              Text(
                "搜索",
                style: TextStyle(fontSize: 12),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTypeSelect() {
    final items = <DropdownMenuItem<int>>[];
    for (var i = 0; i < _typeValues.length; i++) {
      items.add(
        DropdownMenuItem<int>(
          value: i,
          child: Text(_typeNames[i]),
        ),
      );
    }

    selectedBuilder(BuildContext context) {
      final widgets = <Widget>[];
      for (var i = 0; i < _typeValues.length; i++) {
        TextStyle? style;
        if (i == _typeIndex) {
          style = TextStyle(
            color: Theme.of(context).primaryColor,
            fontWeight: FontWeight.bold,
          );
        }
        widgets.add(Text(_typeNames[i], style: style));
      }
      return widgets;
    }

    return Container(
      alignment: Alignment.center,
      // width: 120,
      height: 30,
      // decoration: BoxDecoration(
      //   border: Border.all(),
      //   borderRadius: BorderRadius.circular(8),
      // ),
      child: DropdownButton<int>(
        // underline: SizedBox(),
        items: items,
        value: _typeIndex,
        selectedItemBuilder: selectedBuilder,
        onChanged: (index) {
          if (index != null) {
            setState(() {
              _typeIndex = index;
              _deviceInfo = null;
            });
          }
        },
      ),
    );
  }

  Widget _buildExamCard() {
    final type = _typeValues[_typeIndex];
    switch (type) {
      case "temp":
        return TempCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "weight":
        return WeightCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "sugar":
        return SugarCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "spo2":
        return SpO2Card(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "nibp":
        return NibpCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "heart":
        return HeartCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "urine":
        return UrineCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "ic_reader":
        return ICReaderCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "twelveheart":
        return TwelveHeartCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      case "weight_height":
        return WeightHeightCard(
          mac: _deviceInfo!.mac,
          model: _deviceInfo!.model,
        );
      default:
        return const SizedBox();
    }
  }
}
