import 'package:apps_sidang/common/widget/textField_widget.dart';
import 'package:apps_sidang/generated/assets.dart';
import 'package:apps_sidang/src/home/provider/home_provider.dart';
import 'package:apps_sidang/src/prediction/view/predict_view.dart';
import 'package:awesome_snackbar_content/awesome_snackbar_content.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animated_button/flutter_animated_button.dart';
import 'package:provider/provider.dart';

class HomeView extends StatefulWidget {
  const HomeView({super.key});

  @override
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  final FocusNode _focusNode = FocusNode();
  final FocusNode _focusNode1 = FocusNode();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          "StuntGuard",
          style: TextStyle(fontWeight: FontWeight.w500, fontFamily: 'FiraCode'),
        ),
        actionsPadding: EdgeInsets.symmetric(horizontal: 12),
        actions: [
          Consumer<HomeProvider>(
            builder: (context, homeP, _) {
              return AnimatedButton(
                onPress: () {
                  homeP.clearField();
                  _focusNode.unfocus();
                  _focusNode1.unfocus();
                },
                height: 20,
                width: 100,
                text: 'HAPUS SEMUA',
                isReverse: true,
                selectedTextColor: Colors.white,
                transitionType: TransitionType.CENTER_ROUNDER,
                textStyle: TextStyle(
                  fontWeight: FontWeight.w500,
                  fontSize: 10,
                  color: Colors.black,
                  fontFamily: 'FiraCode',
                ),
                backgroundColor: Colors.white,
                selectedBackgroundColor: Colors.black,
                borderColor: Colors.black,
                borderRadius: 5,
                borderWidth: 1,
              );
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.symmetric(vertical: 8),
          padding: EdgeInsets.symmetric(horizontal: 5),
          child: Consumer<HomeProvider>(
            builder: (context, homeP, _) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CustomTextField.normalTextField(
                    labelText: 'Tinggi Badan (cm)',
                    hintText: 'Masukkan Tinggi Balita',
                    keyboardType: TextInputType.numberWithOptions(
                      decimal: true,
                    ),

                    animation: Image.asset(
                      Assets.assetsImagesCatPeek,
                      width: 40,
                    ),
                    controller: homeP.height,
                    onTap: () {
                      setState(() {
                        homeP.onFocus = false;
                        homeP.onFocus1 = false;
                      });
                    },
                  ),
                  SizedBox(height: 12),
                  CustomTextField.normalTextField(
                    labelText: 'Umur 1 - 60 (bulan)',
                    hintText: 'Masukkan Umur Balita',
                    keyboardType: TextInputType.number,
                    controller: homeP.age,
                    onTap: () {
                      setState(() {
                        homeP.onFocus = false;
                        homeP.onFocus1 = false;
                      });
                    },
                  ),
                  SizedBox(height: 12),
                  CustomTextField.normalTextField(
                    focusNode: _focusNode,
                    labelText: 'Jenis Kelamin',
                    hintText: 'Pilih Jenis Kelamin',
                    controller: TextEditingController(
                      text:
                          homeP.getIndexGender == 0
                              ? 'Laki-Laki'
                              : homeP.getIndexGender == 1
                              ? 'Perempuan'
                              : null,
                    ),
                    keyboardType: TextInputType.number,
                    readOnly: true,
                    onTap: () {
                      if (_focusNode.hasFocus) {
                        _focusNode.unfocus();
                        homeP.onFocus = !homeP.onFocus;
                        setState(() {});
                        return;
                      }
                      setState(() {
                        homeP.onFocus = !homeP.onFocus;
                        homeP.onFocus1 = !homeP.onFocus;
                      });
                    },
                  ),

                  homeP.getOnFocus == true
                      ? Column(
                        children: [
                          SizedBox(height: 10),
                          Container(
                            margin: EdgeInsets.symmetric(horizontal: 12),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10),
                              border: Border.all(color: Colors.black, width: 1),
                            ),
                            child: ListView.separated(
                              shrinkWrap: true,
                              itemBuilder: (context, index) {
                                bool isLastIndex = index == (2 - 1);
                                return GestureDetector(
                                  onTap: () {
                                    if (index == homeP.getIndexGender) {
                                      homeP.indexGender = null;
                                    } else {
                                      homeP.indexGender = index;
                                    }
                                    setState(() {});
                                    print(homeP.getIndexGender);
                                  },
                                  child: Container(
                                    height: 50,
                                    padding: EdgeInsets.all(15),
                                    decoration: BoxDecoration(
                                      color:
                                          homeP.getIndexGender != null
                                              ? homeP.getIndexGender == index
                                                  ? Colors.amber
                                                  : Colors.white
                                              : Colors.white,
                                      borderRadius:
                                          index == 0
                                              ? BorderRadius.only(
                                                topLeft: Radius.circular(10),
                                                topRight: Radius.circular(10),
                                              )
                                              : isLastIndex
                                              ? BorderRadius.only(
                                                bottomLeft: Radius.circular(10),
                                                bottomRight: Radius.circular(
                                                  10,
                                                ),
                                              )
                                              : BorderRadius.zero,
                                    ),
                                    child: Text(
                                      index == 0 ? 'Laki-Laki' : 'Perempuan',
                                      style: TextStyle(
                                        fontSize: 12,
                                        fontWeight: FontWeight.w500,
                                        fontFamily: 'FiraCode',
                                      ),
                                    ),
                                  ),
                                );
                              },
                              separatorBuilder:
                                  (context, index) => Padding(
                                    padding: const EdgeInsets.symmetric(
                                      horizontal: 8,
                                      vertical: 2,
                                    ),
                                    child: Container(
                                      height: 1,
                                      color: Colors.black,
                                    ),
                                  ),
                              itemCount: 2,
                            ),
                          ),
                        ],
                      )
                      : SizedBox(),
                  SizedBox(height: 12),
                  CustomTextField.normalTextField(
                    focusNode: _focusNode1,
                    labelText: 'Riwayat Menyusui',
                    hintText: 'Pilih Riwayat Menyusui',
                    controller: TextEditingController(
                      text:
                          homeP.getIndexBreastfeeding == 0
                              ? 'Tidak'
                              : homeP.getIndexBreastfeeding == 1
                              ? 'Iya'
                              : null,
                    ),
                    keyboardType: TextInputType.number,
                    readOnly: true,
                    onTap: () {
                      if (_focusNode1.hasFocus) {
                        _focusNode1.unfocus();
                        homeP.onFocus1 = !homeP.onFocus1;
                        setState(() {});
                        return;
                      }
                      setState(() {
                        homeP.onFocus1 = !homeP.onFocus1;
                        homeP.onFocus = false;
                      });
                    },
                  ),
                  homeP.getOnFocus1 == true
                      ? Column(
                        children: [
                          SizedBox(height: 10),
                          Container(
                            margin: EdgeInsets.symmetric(horizontal: 12),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10),
                              border: Border.all(color: Colors.black, width: 1),
                            ),
                            child: ListView.separated(
                              shrinkWrap: true,
                              itemBuilder: (context, index) {
                                bool isLastIndex = index == (2 - 1);
                                return GestureDetector(
                                  onTap: () {
                                    if (index == homeP.getIndexBreastfeeding) {
                                      homeP.indexBreastfeeding = null;
                                    } else {
                                      homeP.indexBreastfeeding = index;
                                    }
                                    setState(() {});
                                    print(homeP.getIndexBreastfeeding);
                                  },
                                  child: Container(
                                    height: 50,
                                    padding: EdgeInsets.all(15),
                                    decoration: BoxDecoration(
                                      color:
                                          homeP.getIndexBreastfeeding != null
                                              ? homeP.getIndexBreastfeeding ==
                                                      index
                                                  ? Colors.amber
                                                  : Colors.white
                                              : Colors.white,
                                      borderRadius:
                                          index == 0
                                              ? BorderRadius.only(
                                                topLeft: Radius.circular(10),
                                                topRight: Radius.circular(10),
                                              )
                                              : isLastIndex
                                              ? BorderRadius.only(
                                                bottomLeft: Radius.circular(10),
                                                bottomRight: Radius.circular(
                                                  10,
                                                ),
                                              )
                                              : BorderRadius.zero,
                                    ),
                                    child: Text(
                                      index == 0 ? 'Tidak' : 'Iya',
                                      style: TextStyle(
                                        fontSize: 12,
                                        fontWeight: FontWeight.w500,
                                        fontFamily: 'FiraCode',
                                      ),
                                    ),
                                  ),
                                );
                              },
                              separatorBuilder:
                                  (context, index) => Padding(
                                    padding: const EdgeInsets.symmetric(
                                      horizontal: 8,
                                      vertical: 2,
                                    ),
                                    child: Container(
                                      height: 1,
                                      color: Colors.black,
                                    ),
                                  ),
                              itemCount: 2,
                            ),
                          ),
                        ],
                      )
                      : SizedBox(),

                  SizedBox(height: 25),
                  Padding(
                    padding: const EdgeInsets.all(15),
                    child: Consumer<HomeProvider>(
                      builder: (context, homeP, _) {
                        return AnimatedButton(
                          onPress: () {
                            Future.delayed(Duration(milliseconds: 500), () {
                              if (homeP.height.text == '' ||
                                  homeP.age.text == '' ||
                                  homeP.getIndexGender == null ||
                                  homeP.getIndexBreastfeeding == null) {
                                var snackBar = SnackBar(
                                  elevation: 0,
                                  behavior: SnackBarBehavior.floating,
                                  backgroundColor: Colors.transparent,
                                  content: AwesomeSnackbarContent(
                                    title: 'Ohh Tidak!!',
                                    message:
                                        '${homeP.height.text == ''
                                            ? 'Tinggi badan'
                                            : homeP.age.text == ''
                                            ? 'Umur'
                                            : homeP.getIndexGender == null
                                            ? 'Jenis Kelamin'
                                            : homeP.getIndexBreastfeeding == null
                                            ? 'Riwayat Menyusui'
                                            : '-'} wajib diisi!',
                                    contentType: ContentType.warning,
                                  ),
                                );

                                ScaffoldMessenger.of(context)
                                  ..hideCurrentSnackBar()
                                  ..showSnackBar(snackBar);
                              } else {
                                int intAge = int.parse(homeP.age.text);
                                if (homeP.height.text.contains(',')) {
                                  var snackBar = SnackBar(
                                    elevation: 0,
                                    behavior: SnackBarBehavior.floating,
                                    backgroundColor: Colors.transparent,
                                    content: AwesomeSnackbarContent(
                                      title: 'Ohh Tidak!!',
                                      message:
                                          'Ganti koma (,) menjadi titik (.)',
                                      contentType: ContentType.failure,
                                    ),
                                  );
                                  ScaffoldMessenger.of(context)
                                    ..hideCurrentSnackBar()
                                    ..showSnackBar(snackBar);
                                } else if (intAge > 60 ||
                                    intAge < 1 ||
                                    double.parse(homeP.height.text) > 500.0) {
                                  var snackBar = SnackBar(
                                    elevation: 0,
                                    behavior: SnackBarBehavior.floating,
                                    backgroundColor: Colors.transparent,
                                    content: AwesomeSnackbarContent(
                                      title: 'Ohh Tidak!!',
                                      message:
                                          intAge > 60
                                              ? 'Umur balita tidak boleh lebih dari 60 Bulan!'
                                              : intAge < 1
                                              ? 'Umur balita tidak boleh kurang dari 1 Bulan!'
                                              : double.parse(
                                                    homeP.height.text,
                                                  ) >
                                                  500.0
                                              ? 'Tinggi badan tidak sesuai'
                                              : '-',
                                      contentType: ContentType.failure,
                                    ),
                                  );

                                  ScaffoldMessenger.of(context)
                                    ..hideCurrentSnackBar()
                                    ..showSnackBar(snackBar);
                                } else {
                                  // print('Ini Jalan');
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder:
                                          (context) => PredictView(
                                            height: double.parse(
                                              homeP.getHeight.text,
                                            ),
                                            age: homeP.getAge.text,
                                            gender:
                                                homeP.getIndexGender.toString(),
                                            breastfeeding:
                                                homeP.getIndexBreastfeeding
                                                    .toString(),
                                          ),
                                    ),
                                  );
                                }
                              }
                            });
                          },
                          height: 45,
                          width: double.infinity,
                          text: 'PREDIKSI',
                          isReverse: true,
                          selectedTextColor: Colors.white,
                          transitionType: TransitionType.LEFT_TO_RIGHT,
                          textStyle: TextStyle(
                            fontWeight: FontWeight.w500,
                            fontSize: 12,
                            color: Colors.black,
                            fontFamily: 'FiraCode',
                          ),
                          backgroundColor: Colors.white,
                          selectedBackgroundColor: Colors.black,
                          borderColor: Colors.black,
                          borderRadius: 12,
                          borderWidth: 1,
                        );
                      },
                    ),
                  ),
                  SizedBox(height: 12),
                  // Padding(
                  //   padding: const EdgeInsets.symmetric(horizontal: 12),
                  //   child:
                  // ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}
