import 'dart:convert';
import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_share/flutter_share.dart';
import 'package:healthcare_pharmacy/settings.dart';
import 'package:share/share.dart';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
import 'package:qr_flutter/qr_flutter.dart';



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

  @override
  State<DisplayQrCode> createState() => _DisplayQrCodeState();
}

class _DisplayQrCodeState extends State<DisplayQrCode> {



  Future<void> share(var qr) async {
    await FlutterShare.share(
        title: 'Example share',
        text: 'Example share text',
        linkUrl: qr,
        chooserTitle: 'Example Chooser Title');
  }

  Future<void> shareQRCodeImage1(qrData) async {

    final tempDir = await getTemporaryDirectory();
    final file = File('${tempDir.path}/qr_code.png');
    await file.writeAsBytes(Uint8List.fromList(base64.decode(qrData)));

    // Share the image using the share package
    Share.shareFiles([file.path], text: 'Check out this QR code');

  }

  String imagePath = '';

  Future<void> downloadQRImage() async {
    final response = await http.get(Uri.parse(AppSettings.qrCode));

    if (response.statusCode == 200) {
      final directory = await getApplicationDocumentsDirectory();
      final filePath = '${directory.path}/qr_image.png';

      final File file = File(filePath);
      await file.writeAsBytes(response.bodyBytes);

      setState(() {
        imagePath = filePath;
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppSettings.appBar('Qr Code'),
      body: Container(
        child: Padding(padding: EdgeInsets.all(10),
        child: Column(
          children: [
            Center(
              child: Container(
                padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
                width: MediaQuery.of(context).size.width * .60, // Set the desired width
                height: MediaQuery.of(context).size.height * .35, // Set the desired height
                child:
                Image.memory(Uint8List.fromList(base64.decode(AppSettings.qrCode),),
                    fit: BoxFit.fill),
              ),
            ),
            SizedBox(
              height:MediaQuery.of(context).size.height * .05,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconButton(
                  onPressed: () {
                    //share(AppSettings.qrCode);
                    shareQRCodeImage1(AppSettings.qrCode);
                  },
                  icon: Icon(
                    Icons.share,
                    color: primaryColor,
                    size: 40,
                  ),
                ),
                /*IconButton(
                  onPressed: () {
                    downloadQRImage();
                  },
                  icon: Icon(
                    Icons.download,
                    color: primaryColor,
                    size: 40,
                  ),
                ),*/
              ],
            )
            /*Container(
              padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: Center(child: Image.memory(Uint8List.fromList(base64.decode(AppSettings.qrCode),),),),
            )*/

          ],
        ),),
      ),
    );

  }
}
