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

import 'index.dart';
import 'widgets/wait_card_reader_view.dart';

class BluetoothCardReaderDialog extends GetView<BluetoothCardReaderController> {
  const BluetoothCardReaderDialog({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    const designWidth = 1280.0; // 设计尺寸宽度：1280
    final width = Get.width;
    final scale = width / designWidth; // 计算缩放比例
    final Widget readerView;
    if (controller.isCardReaderConnecting) {
      readerView = const BluetoothWaitCardReaderView();
    } else {
      if (controller.isCardReaderConnected) {
        readerView = const BluetoothCardReaderView();
      } else {
        readerView = const BluetoothNoCardReaderView();
      }
    }
    return Container(
      width: Get.width * 0.6 / scale,
      height: Get.height * 0.6 / scale,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        children: [
          _buildHead(),
          // const Expanded(child: NoCardReaderView()),
          Expanded(child: readerView),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BluetoothCardReaderController>(
      init: BluetoothCardReaderController(),
      id: "bluetooth_card_reader",
      builder: (_) {
        return Dialog(
          child: _buildView(),
        );
      },
    );
  }

  /// 构建弹窗顶部，右侧显示关闭按钮
  Widget _buildHead() {
    return SizedBox(
      height: 50,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          IconButton(
            onPressed: () => Get.back(),
            icon: const Icon(
              Icons.close,
              size: 30,
            ),
          ),
        ],
      ),
    );
  }
}
