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/Sugar/sugar_history.dart';
import 'package:healthcare_user/common/settings.dart';

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

  @override
  State<SugarCalculator> createState() => _SugarCalculatorState();
}

class _SugarCalculatorState extends State<SugarCalculator> {

  TextEditingController fastingValueController = TextEditingController();
  TextEditingController postParandialValueController = TextEditingController();
  String sugarValue = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppSettings.appBar('Diabetes'),
        body: SingleChildScrollView(
          child: Container(
              child: Padding(
                padding: EdgeInsets.all(10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    TextFormField(
                      cursorColor: greyColor,
                      controller: fastingValueController,
                      textCapitalization: TextCapitalization.characters,
                      keyboardType: TextInputType.number,
                      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 fasting sugar value',
                        labelStyle: TextStyle(
                          color: greyColor, //<-- SEE HERE
                        ),
                      ),
                    ),
                    SizedBox(height: 10),
                    TextFormField(
                      cursorColor: greyColor,
                      controller: postParandialValueController,
                      textCapitalization: TextCapitalization.characters,
                      keyboardType: TextInputType.number,
                      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 PostPrandial value',
                        labelStyle: TextStyle(
                          color: greyColor, //<-- SEE HERE
                        ),
                      ),
                    ),
                    SizedBox(height: 10),
                    TextButton(
                      child: Text('Check Sugar', style: textButtonStyle()),
                      onPressed: () async {
                        if (fastingValueController.text != '' &&
                            postParandialValueController.text != '') {
                          AppSettings.preLoaderDialog(context);
                          var payload = new Map<String, dynamic>();

                          payload["fasting"] = double.parse(fastingValueController.text.toString());
                          payload["postPrandial"] = double.parse(postParandialValueController.text.toString());

                          var value = await AppSettings.calculateSugar(payload);
                          var valueResponse = jsonDecode(value);
                          print(valueResponse);
                          setState(() {
                            sugarValue = valueResponse['userSugarDetails']['sugarCategory'].toString();

                          });
                          fastingValueController.clear();
                          postParandialValueController.clear();
                          Navigator.of(context,rootNavigator: true).pop();
                        }
                      },
                    ),
                    SizedBox(height: 10),
                    Container(
                        child: Row(
                          children: [
                            Text(sugarValue,style: TextStyle(color: Colors.red),),
                          ],
                        )
                    ),
                    SizedBox(height: 20),
                    IconButton(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) =>  SugarHistory()),
                        );
                      },
                      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,)),
                      ),)

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