import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:healthcare_user/BMI/bmi_history.dart';
import 'package:healthcare_user/common/settings.dart';

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

  @override
  State<BMICalculator> createState() => _BMICalculatorState();
}

class _BMICalculatorState extends State<BMICalculator> {

  TextEditingController heightController = TextEditingController();
  TextEditingController weightController = TextEditingController();
  TextEditingController ageController = TextEditingController();
  String bmiValue = '';
  String bmiText = '';
  var heightUnitItems = [
    'feet',
    'cm',
    'inches',
  ];
  var heightUnits = 'feet';
  var weightUnitItems = [
    'kg',
    'gr',
  ];
  var weightUnits = 'kg';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppSettings.appBar('Calculate BMI'),
      body: SingleChildScrollView(
        child: Container(
            child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  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();
                          if(double.parse(bmiValue)<18.5){
                            bmiText='Underweight';
                          }
                          else if(double.parse(bmiValue)>=18.5&&double.parse(bmiValue)<=24.9){
                            bmiText='Normal weight';
                          }
                          else if(double.parse(bmiValue)>=25&&double.parse(bmiValue)<=29.9){
                            bmiText='Overweight';
                          }
                          else if(double.parse(bmiValue)>=30){
                            bmiText='Obesity';
                          }
                        });
                      }
                    },
                  ),
                  SizedBox(height: 10),
                  Container(
                      child: Row(
                        children: [
                          Text('Your Bmi value: $bmiValue'),
                          SizedBox(width: 10,),
                          Text(bmiText,style: TextStyle(color: Colors.red)),
                        ],
                      )
                  ),
                  SizedBox(height: 30),
                  Container(
                    child: Text('Underweight = <18.5',style: bmiTextStyle(),),
                  ),
                  SizedBox(height: 10),
                  Container(
                    child: Text('Normal weight = 18.5–24.9',style: bmiTextStyle()),
                  ),
                  SizedBox(height: 10),
                  Container(
                    child: Text('Overweight = 25–29.9',style: bmiTextStyle()),
                  ),
                  SizedBox(height: 10),
                  Container(
                    child: Text('Obesity = BMI of 30 or greater',style: bmiTextStyle()),
                  ),
                  SizedBox(height: 20),
                  IconButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>  BMIHistory()),
                      );
                    },
                    icon: Icon(
                      Icons.history,
                      color: primaryColor,
                      size: 40,
                    ),
                  ),
                  SizedBox(height: 10),
                  Padding(padding: EdgeInsets.fromLTRB(10,0,0,0),
                  child: Container(
                    child: Text('History',style:TextStyle(color:Colors.black,fontSize: 12,fontWeight: FontWeight.bold,)),
                  ),)

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