import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:healthcare_user/BMI/bmi_history.dart';
import 'package:healthcare_user/BP/bp_calculator.dart';
import 'package:healthcare_user/BP/bp_history.dart';
import 'package:healthcare_user/Sugar/sugar_calculator.dart';
import 'package:healthcare_user/Sugar/sugar_history.dart';
import 'package:healthcare_user/common/settings.dart';

import 'BMI/bmi_caluculator.dart';

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

  @override
  State<MyHealth> createState() => _MyHealthState();
}

class _MyHealthState extends State<MyHealth> {
  TextEditingController heightController = TextEditingController();
  TextEditingController weightController = TextEditingController();
  TextEditingController ageController = TextEditingController();
  String bmiValue = '';
  var heightUnitItems = [
    'feet',
    'cm',
    'inches',
  ];
  var heightUnits = 'feet';
  var weightUnitItems = [
    'kg',
    'gr',
  ];
  var weightUnits = 'kg';

  showBMIAdddialog() {
    return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
          return AlertDialog(
            title: Center(
              child: Text(
                'Body Mass Index',
                style: TextStyle(color: primaryColor),
              ),
            ),
            content: SingleChildScrollView(
              child: ListBody(
                children: <Widget>[
                  TextFormField(
                    cursorColor: greyColor,
                    controller: ageController,
                    textCapitalization: TextCapitalization.characters,
                    decoration: const InputDecoration(
                      prefixIcon: Icon(
                        Icons.person,
                        color: primaryColor,
                      ),
                      border: OutlineInputBorder(
                          borderSide: BorderSide(color: primaryColor)),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: primaryColor),
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: primaryColor),
                      ),
                      labelText: 'Age',
                      labelStyle: TextStyle(
                        color: greyColor, //<-- SEE HERE
                      ),
                    ),
                  ),
                  SizedBox(height: 10),
                  Container(
                    //height: 60,
                    child: Row(
                      children: [
                        Expanded(
                          child: TextFormField(
                            cursorColor: greyColor,
                            controller: heightController,
                            textCapitalization: TextCapitalization.characters,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.height,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Height',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),
                          ),
                        ),
                        SizedBox(width: 5),
                        Expanded(
                          child: DropdownButtonFormField(
                            // Initial Value
                            value: heightUnits,
                            isExpanded: true,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.ac_unit_outlined,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Units',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),

                            hint: Text('Units'),
                            // Down Arrow Icon
                            //icon: const Icon(Icons.keyboard_arrow_down),

                            // Array list of items
                            items: heightUnitItems.map((String items) {
                              return DropdownMenuItem(
                                  value: items,
                                  child: Text(
                                    items,
                                    style: TextStyle(
                                      fontSize: 16,
                                    ),
                                    textAlign: TextAlign.center,
                                  ));
                            }).toList(),
                            // After selecting the desired option,it will
                            // change button value to selected value
                            onChanged: (String? newValue) {
                              setState(() {
                                heightUnits = newValue!;
                              });
                            },
                          ),
                        )
                      ],
                    ),
                  ),
                  SizedBox(height: 10),
                  Container(
                    //height: 40,
                    padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                    child: Row(
                      children: [
                        Expanded(
                          child: TextFormField(
                            cursorColor: greyColor,
                            controller: weightController,
                            textCapitalization: TextCapitalization.characters,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.line_weight_outlined,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Weight',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),
                          ),
                        ),
                        SizedBox(width: 5),
                        Expanded(
                          child: DropdownButtonFormField(
                            // Initial Value
                            value: weightUnits,
                            isExpanded: true,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.ac_unit_outlined,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Units',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),

                            hint: Text('Units'),
                            // Down Arrow Icon
                            //icon: const Icon(Icons.keyboard_arrow_down),

                            // Array list of items
                            items: weightUnitItems.map((String items) {
                              return DropdownMenuItem(
                                  value: items,
                                  child: Text(
                                    items,
                                    style: TextStyle(
                                      fontSize: 16,
                                    ),
                                    textAlign: TextAlign.center,
                                  ));
                            }).toList(),
                            // After selecting the desired option,it will
                            // change button value to selected value
                            onChanged: (String? newValue) {
                              setState(() {
                                weightUnits = newValue!;
                              });
                            },
                          ),
                        )
                      ],
                    ),
                  ),
                  SizedBox(height: 10),
                  TextButton(
                    child: Text('Calculate BMI', style: textButtonStyle()),
                    onPressed: () async {
                      if (ageController.text != '' &&
                          heightController.text != '' &&
                          weightController.text != '') {
                        var payload = new Map<String, dynamic>();

                        payload["age"] =
                            int.parse(ageController.text.toString());
                        payload["height"] = heightController.text.toString();
                        payload["weight"] = weightController.text.toString();
                        payload["heightUnit"] = heightUnits.toString();
                        payload["weightUnit"] = weightUnits.toString();
                        var value = await AppSettings.calculateBmi(payload);
                        var valueResponse = jsonDecode(value);
                        print(valueResponse);
                        setState(() {
                          bmiValue = valueResponse['userDetails']['bmivalue']
                              .toString();
                        });
                      }
                    },
                  ),
                  SizedBox(height: 10),
                  Container(
                    child: Text('Your Bmi value: $bmiValue'),
                  ),
                ],
              ),
            ),
            actions: <Widget>[
              TextButton(
                child: Text('Cancel', style: textButtonStyle()),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              TextButton(
                child: Text('Save', style: textButtonStyle()),
                onPressed: () async {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppSettings.appBar('My Health'),
      body: Container(
        child: Column(
          children: [
            Card(
                child: Padding(
              padding: EdgeInsets.all(3),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Body Mass Index'),
                  IconButton(
                    onPressed: () {
                       Navigator.push(
                       context,
                       MaterialPageRoute(
                           builder: (context) =>  BMIHistory()),
                     );
                    },
                    icon: Icon(
                      Icons.history,
                      color: greyColor,
                    ),
                  ),
                  IconButton(
                    onPressed: () {
                       Navigator.push(
                       context,
                       MaterialPageRoute(
                           builder: (context) =>  BMICalculator()),
                     );
                     // showBMIAdddialog();
                    },
                    icon: Icon(
                      Icons.add,
                      color: greyColor,
                    ),
                  )
                ],
              ),
            )),
            Card(
                child: Padding(
              padding: EdgeInsets.all(3),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Blood Pressure'),
                  IconButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>  BPHistory()),
                      );
                    },
                    icon: Icon(
                      Icons.history,
                      color: greyColor,
                    ),
                  ),
                  IconButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>  BPCalculator()),
                      );
                      // showBMIAdddialog();
                    },
                    icon: Icon(
                      Icons.add,
                      color: greyColor,
                    ),
                  )
                ],
              ),
            )),
            Card(
                child: Padding(
              padding: EdgeInsets.all(3),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Diabetes'),
                  IconButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>  SugarHistory()),
                      );
                    },
                    icon: Icon(
                      Icons.history,
                      color: greyColor,
                    ),
                  ),
                  IconButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>  SugarCalculator()),
                      );
                      // showBMIAdddialog();
                    },
                    icon: Icon(
                      Icons.add,
                      color: greyColor,
                    ),
                  )
                ],
              ),
            )),
            Card(
                child: Padding(
              padding: EdgeInsets.all(3),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Cholesterol'),
                  IconButton(
                    onPressed: () {},
                    icon: Icon(
                      Icons.add,
                      color: greyColor,
                    ),
                  )
                ],
              ),
            )),
            Card(
                child: Padding(
              padding: EdgeInsets.all(3),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Habbits'),
                  IconButton(
                    onPressed: () {},
                    icon: Icon(
                      Icons.add,
                      color: greyColor,
                    ),
                  )
                ],
              ),
            )),
            Card(
                child: Padding(
              padding: EdgeInsets.all(3),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Allergies'),
                  IconButton(
                    onPressed: () {},
                    icon: Icon(
                      Icons.add,
                      color: greyColor,
                    ),
                  )
                ],
              ),
            ))
          ],
        ),
      ),
    );
  }
}
