import 'dart:convert';

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

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

  @override
  State<BPCalculator> createState() => _BPCalculatorState();
}

class _BPCalculatorState extends State<BPCalculator> {

  TextEditingController systoloicController = TextEditingController();
  TextEditingController diastolicController = TextEditingController();
  String bpValue = '';
  String bpText = '';
  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('Blood Pressure'),
        body: SingleChildScrollView(
          child: Container(
              child: Padding(
                padding: EdgeInsets.all(10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    TextFormField(
                      cursorColor: greyColor,
                      controller: systoloicController,
                      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: 'Enter systolic value',
                        labelStyle: TextStyle(
                          color: greyColor, //<-- SEE HERE
                        ),
                      ),
                    ),
                    SizedBox(height: 10),
                    TextFormField(
                      cursorColor: greyColor,
                      controller: diastolicController,
                      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: 'Enter diastolic value',
                        labelStyle: TextStyle(
                          color: greyColor, //<-- SEE HERE
                        ),
                      ),
                    ),
                    SizedBox(height: 10),
                    TextButton(
                      child: Text('Check BP', style: textButtonStyle()),
                      onPressed: () async {
                        if (systoloicController.text != '' &&
                            diastolicController.text != '') {
                          AppSettings.preLoaderDialog(context);
                          var payload = new Map<String, dynamic>();

                          payload["Systolic"] = double.parse(systoloicController.text.toString());
                          payload["Diastolic"] = double.parse(diastolicController.text.toString());

                          var value = await AppSettings.calculateBP(payload);
                          var valueResponse = jsonDecode(value);
                          print(valueResponse);
                          setState(() {
                            bpValue = valueResponse['userDetails']['bpCategory'].toString();
                            /*bpValue = valueResponse['userDetails']['bmivalue'].toString();
                            if(double.parse(bpValue)<18.5){
                              bpText='Underweight';
                            }
                            else if(double.parse(bpValue)>=18.5&&double.parse(bpValue)<=24.9){
                              bpText='Normal weight';
                            }
                            else if(double.parse(bpValue)>=25&&double.parse(bpValue)<=29.9){
                              bpText='Overweight';
                            }
                            else if(double.parse(bpValue)>=30){
                              bpText='Obesity';
                            }*/
                          });
                          systoloicController.clear();
                          diastolicController.clear();
                          Navigator.of(context,rootNavigator: true).pop();
                        }
                      },
                    ),
                    SizedBox(height: 10),
                    Container(
                        child: Row(
                          children: [
                            Text(bpValue,style: TextStyle(color: Colors.red),),
                          ],
                        )
                    ),
                    SizedBox(height: 20),
                    IconButton(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) =>  BPHistory()),
                        );
                      },
                      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,)),
                      ),)

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