import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthcare_user/common/settings.dart';
import 'package:image_picker/image_picker.dart';

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

  @override
  State<ReportMySelf> createState() => _ReportMySelfState();
}

class _ReportMySelfState extends State<ReportMySelf> {

  TextEditingController problemNameController = TextEditingController();
  TextEditingController descriptionController = TextEditingController();
  final ImagePicker _picker = ImagePicker();
  String videoUrl='';

  Future takeVideoFromCamera() async {
    try {
      final image = await _picker.pickVideo(source: ImageSource.camera);
      if (image == null) return;
      final imageTemp = File(image.path);
      AppSettings.preLoaderDialog(context);
      var res = await AppSettings.uploadVideoInReportMySelf(image);
      print(jsonDecode(res));
      Navigator.of(context, rootNavigator: true).pop();
      setState(() {
        videoUrl = jsonDecode(res)['picture'];
      });
    } on PlatformException catch (e) {
      print('Failed to pick video: $e');
    }
  }

  Future pickVideoFromGallery() async {
    try {
      final image = await _picker.pickVideo(source: ImageSource.gallery);
      if (image == null) return;
      final imageTemp = File(image.path);

      AppSettings.preLoaderDialog(context);
      var res = await AppSettings.uploadVideoInReportMySelf(image);
      print(jsonDecode(res));
      Navigator.of(context, rootNavigator: true).pop();
      setState(() {
        videoUrl = jsonDecode(res)['picture'];
      });
    } on PlatformException catch (e) {
      print('Failed to pick video: $e');
    }
  }


  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppSettings.appBar('Report MySelf'),
      body: Container(
        child: Padding(
          padding: EdgeInsets.all(10),
          child:Column(
            children: [
              Container(
                padding: const EdgeInsets.all(10),
                child: TextFormField(
                  cursorColor: greyColor,
                  controller: problemNameController,
                  keyboardType: TextInputType.number,
                  decoration: const InputDecoration(
                    prefixIcon: Icon(
                      Icons.phone,
                      color: greyColor,
                    ),
                    border: OutlineInputBorder(
                        borderSide: BorderSide(color: primaryColor)),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color:primaryColor),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: primaryColor),
                    ),
                    labelText: 'Enter problem name',
                    labelStyle: TextStyle(
                      color: greyColor, //<-- SEE HERE
                    ),
                  ),
                ),
              ),
              const SizedBox(
                height: 15,
              ),
              Container(
                padding: const EdgeInsets.all(10),
                child: TextFormField(
                  cursorColor: greyColor,
                  controller: descriptionController,
                  keyboardType: TextInputType.number,
                  decoration: const InputDecoration(
                    prefixIcon: Icon(
                      Icons.description,
                      color: greyColor,
                    ),
                    border: OutlineInputBorder(
                        borderSide: BorderSide(color: primaryColor)),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color:primaryColor),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: primaryColor),
                    ),
                    labelText: 'Description',
                    labelStyle: TextStyle(
                      color: greyColor, //<-- SEE HERE
                    ),
                  ),
                ),
              ),
              Row(
                children: [

                  IconButton(
                    icon:  Icon(
                      Icons.audio_file,
                      color: primaryColor,
                    ),
                    onPressed: () {

                    },
                  ),
                  SizedBox(width: 10,),
                  IconButton(
                    icon:  Icon(
                      Icons.video_camera_back_rounded,
                      color: primaryColor,
                    ),
                    onPressed: () {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return SizedBox(
                              height: 200,
                              child: Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Icon(
                                        Icons.camera_alt_outlined,
                                        size: 100,
                                        color: greyColor,
                                      ),
                                      onTap: () async {
                                        await takeVideoFromCamera();
                                        Navigator.pop(context);
                                      },
                                    ),
                                    SizedBox(
                                      width: MediaQuery.of(context).size.width * .20,
                                    ),
                                    GestureDetector(
                                      child: Icon(
                                        Icons.photo,
                                        size: 100,
                                        color: greyColor,
                                      ),
                                      onTap: () async {
                                        await pickVideoFromGallery();
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            );
                          });
                    },
                  ),
                  SizedBox(width: 10,),
                  IconButton(
                    icon:  Icon(
                      Icons.image,
                      color: primaryColor,
                    ),
                    onPressed: () {

                    },
                  ),
                ],
              ),
              Visibility(
                visible: videoUrl != '',
                child: Padding(
                  padding: EdgeInsets.fromLTRB(10,0,0,0),
                  child: Container(
                    width: MediaQuery.of(context).size.width * .18,
                    height: MediaQuery.of(context).size.height * .10,
                    decoration: BoxDecoration(
                        shape: BoxShape.rectangle,
                        image: DecorationImage(
                            image: NetworkImage(videoUrl) as ImageProvider, // picked file
                            fit: BoxFit.contain)),
                  ),

                ),
              ),


            ],
          ),
        ),
      ),
    );
  }
}
