import 'dart:ui';

import 'package:flutter/material.dart';
// import 'package:flutter_scankit/scan_kit_widget.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:scan/scan.dart';

class QrCodeScanPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _QrCodeScanPage();
  }
}

class _QrCodeScanPage extends State<QrCodeScanPage> {

  ScanController controller = ScanController();
  final screenWidth = window.physicalSize.width;
  final screenHeight = window.physicalSize.height;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Stack(
          children: [
            Positioned(
              child: Column(
                children: [
                  Expanded(
                    child: ScanView(
                      controller: controller,
                      scanAreaScale: 0.7,
                      scanLineColor: Colors.green.shade400,
                      onCapture: (data) {
                        if (data != null && data != "") {
                          Navigator.of(context).pop(data);
                        }
                      },
                    ),
                    flex: 1,
                  ),
                ],
              ),
              top: 0,
              bottom: 0,
              left: 0,
              right: 0,
            ),
            Positioned(
              child: MyAppBar(
                title: "扫码",
                titleColor: Colors.white,
                leadingColor: Colors.white,
                brightness: Brightness.dark,
                background: Colors.transparent,
                action: Container(
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(right: 16.w),
                  child: GestureDetector(
                    onTap: () {
                      pickImage();
                    },
                    child: Text(
                      S.of(context).xiangce,
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 18.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ),
              top: 0,
              left: 0,
              right: 0,
            ),
          ],
        ),
      ),
    );
  }

  pickImage() async {
    List<Media> medias = await ImagePickers.pickerPaths(
      galleryMode: GalleryMode.image,
      selectCount: 1,
      showGif: true,
      showCamera: false,
      compressSize: 500,
      uiConfig: UIConfig(
        uiThemeColor: Color(0xFFFFFFFF),
      ),
      cropConfig: CropConfig(
        enableCrop: false,
      ),
    );
    if (medias != null && medias.length > 0) {
      String result = await Scan.parse(medias[0].path);
      if (result != null && result != "") {
        Navigator.of(context).pop(result);
      }
    }
  }

  @override
  void dispose() {
    if (this.controller != null) {
      this.controller.pause();
    }
    super.dispose();
  }
}