import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:img_picker/img_picker.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/model/api_response.dart';
import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart';

class MobileScannerTestPage extends StatefulWidget {
  const MobileScannerTestPage({Key? key}) : super(key: key);

  @override
  State<MobileScannerTestPage> createState() => _MobileScannerTestPageState();
}

class _MobileScannerTestPageState extends State<MobileScannerTestPage>
    with TickerProviderStateMixin {
  String? scannedText;
  bool isScanning = true;

  late AnimationController _controller;
  late Animation<double> _animation;
  late MobileScannerController _scannerController;
  BlueteethBindController blueteethBindController = Get.find();

  final double scanAreaSize = 480.rpx;

  @override
  void initState() {
    super.initState();
    _scannerController = MobileScannerController();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )..repeat(reverse: true);

    _animation = Tween<double>(begin: 0, end: 1).animate(CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    ));
  }

  @override
  void dispose() {
    _controller.dispose();
    _scannerController.dispose();
    super.dispose();
  }

  void _onDetect(BarcodeCapture capture) {
    if (!isScanning) return;

    final Barcode? barcode = capture.barcodes.first;
    final String? value = barcode?.rawValue;

    if (value != null) {
      setState(() {
        scannedText = value;
        isScanning = false;
        if (scannedText != null && scannedText!.isNotEmpty) {
          blueteethBindController.scanMac.value = scannedText!;
          showConfirmDialog(
            context,
            Container(),
            '蓝牙绑定.确定绑定提示'.tr,
            onConfirm: () async {
              ApiResponse response =
                  await blueteethBindController.bindDeviceByScan(scannedText!);
              if (response.code == HttpStatusCodes.ok) {
                TopSlideNotification.show(
                  context,
                  text: "蓝牙绑定.连接成功".tr,
                  textColor: themeController.currentColor.sc2,
                );
              } else {
                TopSlideNotification.show(
                  context,
                  text: response.msg ?? "蓝牙绑定.连接异常".tr,
                  textColor: themeController.currentColor.sc9,
                );
              }
            },
            onCancel: () {
              print('用户点击了取消');
              // 执行取消后的处理逻辑
            },
          );
        }
      });

      Future.delayed(const Duration(seconds: 2), () {
        setState(() {
          isScanning = true;
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      appBar: AppBar(
        backgroundColor: themeController.currentColor.sc17,
        automaticallyImplyLeading: false,
        iconTheme: IconThemeData(color: themeController.currentColor.sc3),
        titleSpacing: 0,
        title: Container(
          width: double.infinity,
          height: 180.rpx,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Text(
                '扫一扫.标题'.tr,
                style: TextStyle(
                  fontFamily: 'Readex Pro',
                  color: themeController.currentColor.sc3,
                  letterSpacing: 0,
                  fontSize: 30.rpx,
                ),
              ),
              Positioned(
                left: 0,
                child: returnIconButtom,
              ),
            ],
          ),
        ),
        centerTitle: false,
      ),
      body: Container(
        child: Column(
          children: [
            Expanded(
              child: Stack(
                children: [
                  MobileScanner(
                    controller: _scannerController,
                    onDetect: _onDetect,
                  ),
                  Align(
                    alignment: Alignment.topCenter, // 使扫描框位于顶部居中
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(top: 219.rpx), // 向上移动扫描框
                          child: SizedBox(
                            width: scanAreaSize,
                            height: scanAreaSize,
                            child: Stack(
                              children: [
                                Container(
                                  decoration: BoxDecoration(
                                    color: themeController.currentColor.sc5,
                                  ),
                                ),
                                AnimatedBuilder(
                                  animation: _animation,
                                  builder: (context, child) {
                                    return Positioned(
                                      top: scanAreaSize * _animation.value,
                                      left: 0,
                                      right: 0,
                                      child: Container(
                                        height: 2,
                                        color: themeController.currentColor.sc2,
                                      ),
                                    );
                                  },
                                ),
                              ],
                            ),
                          ),
                        ),
                        SizedBox(height: 31.rpx),
                        Text(
                          '扫一扫.提示'.tr,
                          style: TextStyle(
                            color: themeController.currentColor.sc2,
                            fontSize: 26.rpx,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(0, 0, 0, 83.rpx),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  GestureDetector(
                    onTap: () async {
                      final picker = ImagePicker();
                      final pickedFile =
                          await picker.pickImage(source: ImageSource.gallery);
                      if (pickedFile != null) {
                        final bytes = await pickedFile.readAsBytes();
                        final image = await decodeImageFromList(bytes);
                      }
                    },
                    child: Column(
                      children: [
                        Icon(Icons.photo,
                            color: themeController.currentColor.sc2,
                            size: 60.rpx),
                        SizedBox(height: 10.rpx),
                        Text(
                          '扫一扫.相册'.tr,
                          style: TextStyle(
                            color: themeController.currentColor.sc2,
                            fontSize: 24.rpx,
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(width: 80.rpx),
                  GestureDetector(
                    onTap: () {
                      _scannerController.toggleTorch();
                    },
                    child: Column(
                      children: [
                        Icon(Icons.flashlight_on,
                            color: themeController.currentColor.sc2,
                            size: 60.rpx),
                        SizedBox(height: 10.rpx),
                        Text(
                          '扫一扫.手电筒'.tr,
                          style: TextStyle(
                            color: themeController.currentColor.sc2,
                            fontSize: 24.rpx,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
