import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/health_calculator/calorie_calculator/calorie_calculator.dart';
import 'package:diplomaticquarterapp/widgets/buttons/button.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'carbs_result_page.dart';

class Carbs extends StatefulWidget {
  @override
  _CarbsState createState() => _CarbsState();
}

class _CarbsState extends State<Carbs> {
  TextEditingController textController = new TextEditingController();
  int calories;
  String dropdownValue;
  bool _visible = false;
  int meals;
  int protein;
  int carbs;

  int fat;
  double pCal;
  double cCal;
  double fCal;
  double pCalGram;
  double cCalGram;
  double fCalGram;
  double pCalMeal;
  double cCalMeal;
  double fCalMeal;

  void calculateDietRatios() {
    if (dropdownValue == 'Very Low Carb') {
      meals = 3;
      protein = 45;
      carbs = 10;
      fat = 45;
    } else if (dropdownValue == 'Low Carb') {
      meals = 3;
      protein = 40;
      carbs = 30;
      fat = 30;
    } else if (dropdownValue == 'Moderate Carb') {
      meals = 3;
      protein = 25;
      carbs = 50;
      fat = 25;
    } else if (dropdownValue == 'USDA Gudilines') {
      meals = 3;
      protein = 15;
      carbs = 55;
      fat = 30;
    } else if (dropdownValue == 'Zone Diet') {
      meals = 3;
      protein = 30;
      carbs = 40;
      fat = 30;
    }
  }

  void calculate() {
    pCal = (protein / 100.0) * int.parse(textController.text).ceil();
    cCal = (carbs / 100.0) * int.parse(textController.text);
    ;
    fCal = (fat / 100) * int.parse(textController.text);
    ;
    pCalGram = pCal / 4.0;
    cCalGram = cCal / 4.0;
    fCalGram = fCal / 9.0;
    pCalMeal = pCalGram / meals.ceil();
    cCalMeal = cCalGram / meals.ceil();
    fCalMeal = fCalGram / meals.ceil();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      appBarTitle: 'Carb Protein Fat',
      showHomeAppBarIcon: false,
      appBarIcons: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 7.0),
          child: Icon(
            Icons.info_outline,
            color: Colors.white,
          ),
        )
      ],
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 25.0, vertical: 15.0),
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Texts(
                  'Calculates carbohydrate protein and fat\n ratio in calories and grams according to a\n pre-set ratio',
                ),
                SizedBox(
                  height: 15.0,
                ),
                Column(
                  children: [
                    Container(
                      width: 340.0,
                      height: 60.0,
                      decoration: BoxDecoration(
                        color: Colors.white,
                      ),
                      child: Row(
                        children: [
                          Padding(
                            padding: EdgeInsets.symmetric(
                                vertical: 10.0, horizontal: 8.0),
                            child: Center(
                              child: Container(
                                width: 300.0,
                                foregroundDecoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(5.0),
                                  border: Border.all(
                                    color: Colors.blueGrey,
                                    width: 2.0,
                                  ),
                                ),
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: Center(
                                        child: TextFormField(
                                          controller: textController,
                                          inputFormatters: <TextInputFormatter>[
                                            FilteringTextInputFormatter
                                                .digitsOnly
                                          ],
                                          keyboardType: TextInputType.number,
                                          decoration: InputDecoration(
                                            hintText: " The Calories per day ",
                                            labelStyle: TextStyle(
                                              color: Colors.black87,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                    Container(
                                      height: 38.0,
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: <Widget>[
                                          Container(
                                            decoration: BoxDecoration(
                                              border: Border(
                                                bottom: BorderSide(
                                                  width: 0.5,
                                                ),
                                              ),
                                            ),
                                            child: InkWell(
                                              child: Icon(
                                                Icons.arrow_drop_up,
                                                size: 18.0,
                                              ),
                                              onTap: () {
                                                setState(() {
                                                  int currentValue = int.parse(
                                                      textController.text);
                                                  currentValue++;
                                                  textController.text =
                                                      (currentValue).toString();
                                                });
                                              },
                                            ),
                                          ),
                                          InkWell(
                                            child: Icon(
                                              Icons.arrow_drop_down,
                                              size: 18.0,
                                            ),
                                            onTap: () {
                                              setState(() {
                                                int currentValue = int.parse(
                                                    textController.text);
                                                currentValue--;
                                                textController.text =
                                                    (currentValue).toString();
                                              });
                                            },
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    Button(
                      backgroundColor: Color(0xffC5272D),
                      label: 'NOT SURE? CLICK HERE',
                      onTap: () {
                        setState(() {
                          {
                            Navigator.push(
                              context,
                              FadePage(page: CalorieCalculator()),
                            );
                          }
                        });
                      },
                    ),
                  ],
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Texts('Select Diet Type'),
                    Container(
                      width: 400,
                      child: DropdownButton<String>(
                        value: dropdownValue,
                        icon: Icon(Icons.arrow_downward),
                        iconSize: 24,
                        elevation: 16,
                        style: TextStyle(color: Colors.black87),
                        underline: Container(
                          height: 2,
                          color: Colors.black54,
                        ),
                        onChanged: (String newValue) {
                          setState(() {
                            dropdownValue = newValue;
                            calculateDietRatios();

                            dropdownValue == null
                                ? _visible = false
                                : _visible = true;
                          });
                        },
                        items: <String>[
                          'Very Low Carb',
                          'Low Carb',
                          'Moderate Carb',
                          'USDA Gudilines',
                          'Zone Diet',
                        ].map<DropdownMenuItem<String>>((String value) {
                          return DropdownMenuItem<String>(
                            value: value,
                            child: Text(value),
                          );
                        }).toList(),
                      ),
                    ),
                    Visibility(
                      visible: _visible,
                      child: Container(
                        height: 170.0,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Texts(
                                'Ratios are divided according to the selected diet'),
                            RichText(
                              text: TextSpan(
                                style: TextStyle(color: Colors.black),
                                children: [
                                  TextSpan(text: 'Meals Per Day '),
                                  TextSpan(
                                    text: '$meals',
                                    style: TextStyle(color: Color(0xffC5272D)),
                                  ),
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                style: TextStyle(color: Colors.black),
                                children: [
                                  TextSpan(
                                    text: 'Protein   ',
                                  ),
                                  TextSpan(
                                    text: '$protein%',
                                    style: TextStyle(color: Color(0xffC5272D)),
                                  )
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                style: TextStyle(color: Colors.black),
                                children: [
                                  TextSpan(
                                    text: 'Carbohydrate   ',
                                  ),
                                  TextSpan(
                                    text: '$carbs%',
                                    style: TextStyle(color: Color(0xffC5272D)),
                                  )
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                style: TextStyle(color: Colors.black),
                                children: [
                                  TextSpan(
                                    text: 'Fat   ',
                                  ),
                                  TextSpan(
                                    text: '$fat%',
                                    style: TextStyle(color: Color(0xffC5272D)),
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
                SizedBox(
                  height: 55.0,
                ),
                Container(
                  height: 50.0,
                  width: 350.0,
                  child: SecondaryButton(
                    label: 'CALCULATE',
                    onTap: () {
                      setState(() {
                        {
                          calculate();
                          Navigator.push(
                            context,
                            FadePage(
                                page: CarbsResult(
                              cCal: cCal,
                              pCal: pCal,
                              fCal: fCal,
                              pCalGram: pCalGram,
                              pCalMeal: pCalMeal,
                              fCalGram: fCalGram,
                              fCalMeal: fCalMeal,
                              cCalGram: cCalGram,
                              cCalMeal: cCalMeal,
                            )),
                          );
                        }
                      });
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
