import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:healthcare_user/emergency.dart';
import 'package:healthcare_user/invitations.dart';
import 'package:healthcare_user/medicines.dart';
import 'package:healthcare_user/prescriptions.dart';
import 'package:healthcare_user/reports.dart';
import 'package:healthcare_user/seekopinion.dart';
import 'package:healthcare_user/settings.dart';
import 'package:image_picker/image_picker.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'dart:ui' as ui;
import 'login.dart';

class Dashboard extends StatefulWidget {
  const Dashboard({super.key});

  @override
  State<Dashboard> createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  int _selectedIndex = 0;
  final List<String> imgList = [
    'images/mobilebg.png',
    'images/mobilebg.png',
    'images/mobilebg.png'
  ];

  final ImagePicker _picker = ImagePicker();
  bool isTablet = false;
  bool isPhone = true;

  final double devicePixelRatio = ui.window.devicePixelRatio;
  static final ui.Size size1 = ui.window.physicalSize;
  final double width = size1.width;
  final double height = size1.height;

  devicedetection() {
    if (devicePixelRatio < 2 && (width >= 1000 || height >= 1000)) {
      isTablet = true;
      isPhone = false;
    } else if (devicePixelRatio == 2 && (width >= 1920 || height >= 1920)) {
      isTablet = true;
      isPhone = false;
    } else {
      isTablet = false;
      isPhone = true;
    }
  }

  List<String> images = [
    "images/seekopinion.png",
    "images/reports.png",
    "images/prescriptions.png",
    "images/medicines.png",
    "images/invitations.png",
    "images/emergency.png"
  ];

  gridOntap(int ind){

    if(ind==0){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => const SeekOpinion()),
      );
    }
    else if(ind==1){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => const Reports()),
      );
    }
    else if(ind==2){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => const Prescriptions()),
      );
    }
    else if(ind==3){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => const Medicines()),
      );
    }
    else if(ind==4){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => const Invitations()),
      );
    }
    else if(ind==5){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => const Emergency()),
      );
    }

  }

  Widget _dashBoard() {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          CarouselSlider(
            options: CarouselOptions(
              height: MediaQuery.of(context).size.height * .250,
              aspectRatio: 16 / 9,
              viewportFraction: 0.8,
              initialPage: 0,
              enableInfiniteScroll: true,
              reverse: false,
              autoPlay: true,
              autoPlayInterval: Duration(seconds: 3),
              autoPlayAnimationDuration: Duration(milliseconds: 800),
              autoPlayCurve: Curves.ease,
              enlargeCenterPage: true,
              enlargeFactor: 0.2,
              scrollDirection: Axis.horizontal,
            ),
            items: imgList.map((i) {
              return Builder(
                builder: (BuildContext context) {
                  return Container(
                      height: MediaQuery.of(context).size.height * .250,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(0),
                      ),
                      //color: Colors.red,
                      child: FittedBox(
                        child: Image(
                          image: AssetImage(i),
                        ),
                        fit: BoxFit.fill,
                      ));
                },
              );
            }).toList(),
          ),
          SizedBox(height: 5),
          Expanded(child: Container(
              padding: EdgeInsets.all(12.0),
              child: GridView.builder(
                itemCount: images.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    crossAxisSpacing: 4.0,
                    mainAxisSpacing: 4.0,),
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                    onTap: (){
                      gridOntap(index);
                    },
                    child:  Image.asset(images[index]),
                  );
                },
              )),)
        ],
      ),
    );
  }

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

  showLogoutAlertDialog(context) {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
            return AlertDialog(
                title: const Text('Do you really want to logout?'),
                actions: <Widget>[
                  TextButton(
                    child: Text('No', style: textButtonStyle()),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                  TextButton(
                    child: Text('Yes', style: textButtonStyle()),
                    onPressed: () async {
                      await AppSettings.clearSharedPreferences();
                      await Navigator.pushAndRemoveUntil(
                        context,
                        MaterialPageRoute(builder: (context) => Login()),
                        (Route<dynamic> route) => false,
                      );
                    },
                  ),
                ]);
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        final shouldPop = await showDialog<bool>(
          context: context,
          builder: (context) {
            return AlertDialog(
              title: const Text('Do you want to exit app?',
                  style: TextStyle(
                    color: primaryColor,
                    fontSize: 20,
                  )),
              actionsAlignment: MainAxisAlignment.spaceBetween,
              actions: [
                TextButton(
                  onPressed: () {
                    SystemNavigator.pop();
                  },
                  child: const Text('Yes',
                      style: TextStyle(
                        color: primaryColor,
                        fontSize: 20,
                      )),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                  child: const Text('No',
                      style: TextStyle(
                        color: primaryColor,
                        fontSize: 20,
                      )),
                ),
              ],
            );
          },
        );
        return shouldPop!;
      },
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: AppSettings.appBar('Health Care'),
        drawer: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: [
              DrawerHeader(
                  decoration: const BoxDecoration(
                    color: primaryColor,
                  ),
                  child: Row(
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Expanded(
                            child: GestureDetector(
                              child: Container(
                                width: MediaQuery.of(context).size.width * .20,
                                height:
                                    MediaQuery.of(context).size.height * .15,
                                decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    image: DecorationImage(
                                        image: AssetImage(
                                            "images/profile_pic.png"), // picked file
                                        fit: BoxFit.cover)),
                              ),
                              onTap: () {},
                            ),
                          ),
                          Text(
                            "Sneha",
                            //AppSettings.userName,
                            style: TextStyle(color: Colors.white, fontSize: 15),
                          ),
                          Text(
                            '9898989898',
                            //AppSettings.phoneNumber,
                            style: TextStyle(color: Colors.white, fontSize: 15),
                          ),
                          Text(
                            'sneha@armintasolutions.com', //AppSettings.email,
                            style: TextStyle(color: Colors.white, fontSize: 15),
                          ),
                        ],
                      ),
                      Container()
                    ],
                  )),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('Edit Profile', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('My Health', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('Report Myself', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('My Connections', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('My Medicine Timings', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('Update Pin', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('Update Mobile Number', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('Update My Location', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),
              Divider(
                color: Colors.grey,
              ),
              ListTile(
                title: Row(
                  children: const [
                    Image(
                        image: const AssetImage('images/01.png'),
                        height: 25,
                        width: 25,
                        fit: BoxFit.fill),
                    const SizedBox(
                      width: 10,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('Logout', style: TextStyle(color: primaryColor)),
                  ],
                ),
                onTap: () {},
              ),

            ],
          ),
        ),
        body: _dashBoard(),
      ),
    );
  }
}
