import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:datetime_picker_formfield_new/datetime_picker_formfield.dart';
import 'package:intl/intl.dart';
import 'package:cool_dropdown/models/cool_dropdown_item.dart';
import 'package:cool_dropdown/cool_dropdown.dart';

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

  @override
  State<Forecast> createState() => _ForecastState();
}

class _ForecastState extends State<Forecast> {
  bool showPrediction = true;
  String usd = "--";
  String lkr = "--";
  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    var height = MediaQuery.of(context).size.height;

    final format = DateFormat("yyyy-MM-dd");

    List<CoolDropdownItem<String>> grades = [
      CoolDropdownItem(
        label: "Alba",
        value: "alba",
      ),
      CoolDropdownItem(
        label: "C-5 Sp",
        value: "c5sp",
      ),
      CoolDropdownItem(
        label: "C5",
        value: "c5",
      ),
      CoolDropdownItem(
        label: "M-5",
        value: "m5",
      ),
      CoolDropdownItem(
        label: "M-4",
        value: "m4",
      ),
    ];
    List<CoolDropdownItem<String>> region = [
      CoolDropdownItem(
        label: "Galle",
        value: "galle",
      ),
      CoolDropdownItem(
        label: "Matara",
        value: "matara",
      ),
      CoolDropdownItem(
        label: "Hambantota",
        value: "hambantota",
      ),
    ];
    final gradeDropdownController = DropdownController();

    toggleLoadingAndPredict() {
      setState(() {
        showPrediction = false;
        lkr = "3300";
        usd = "10";
      });

      sleep(Duration(seconds: 4));
      setState(() {
        showPrediction = true;
      });
    }

    return Scaffold(
      backgroundColor: Colors.grey.shade900,
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text(
          "Price Forecast",
          style: GoogleFonts.roboto(
            color: Colors.white,
            fontWeight: FontWeight.bold,
          ),
        ),
        leading: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: Icon(
            Icons.arrow_back,
            color: Colors.white,
          ),
        ),
      ),
      body: Container(
        padding: EdgeInsets.fromLTRB(20, 10, 20, 0),
        child: Column(
          children: [
            Card(
              elevation: 10,
              shadowColor: Colors.black38,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15.0),
              ),
              child: Container(
                  padding: EdgeInsets.symmetric(
                    horizontal: 20,
                    vertical: 10,
                  ),
                  width: width,
                  // height: height / 3,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      DateTimeField(
                        format: format,
                        decoration: InputDecoration(
                          labelText: "Select a day",
                        ),
                        onShowPicker: (context, currentValue) {
                          return showDatePicker(
                            context: context,
                            firstDate: DateTime(
                              DateTime.now().year - 1,
                              DateTime.now().month,
                              DateTime.now().day,
                            ),
                            initialDate: currentValue ?? DateTime.now(),
                            lastDate: DateTime(2100),
                          );
                        },
                      ),
                      SizedBox(
                        height: 20,
                      ),
                      Text(
                        "Select a grade",
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      CoolDropdown<String>(
                          dropdownList: grades,
                          isMarquee: false,
                          resultOptions: ResultOptions(
                            width: width,
                          ),
                          defaultItem:
                              CoolDropdownItem(label: "Alba", value: "alba"),
                          dropdownItemOptions: DropdownItemOptions(),
                          controller: gradeDropdownController,
                          onChange: (value) {
                            print(value);
                          }),
                      SizedBox(
                        height: 20,
                      ),
                      Text(
                        "Select your region",
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      CoolDropdown<String>(
                          dropdownList: region,
                          isMarquee: false,
                          resultOptions: ResultOptions(
                            width: width,
                          ),
                          defaultItem:
                              CoolDropdownItem(label: "Galle", value: "galle"),
                          dropdownItemOptions: DropdownItemOptions(),
                          controller: gradeDropdownController,
                          onChange: (value) {
                            print(value);
                          }),
                      SizedBox(
                        height: 30,
                      ),
                      GestureDetector(
                        onTap: () {
                          setState(() {
                            showPrediction = false;
                            lkr = "3600";
                            usd = "10";
                          });
                          print("ds");
                          // sleep(Duration(seconds: 4));
                          setState(() {
                            showPrediction = true;
                          });
                        },
                        child: Container(
                          width: width,
                          padding: EdgeInsets.symmetric(
                            vertical: 10,
                          ),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            color: Colors.lime.shade700,
                          ),
                          child: Center(
                            child: Text(
                              "Predict",
                              style: GoogleFonts.roboto(
                                color: Colors.white,
                                fontWeight: FontWeight.w400,
                                fontSize: width / 22,
                              ),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),

                    ],
                  )),
            ),
            SizedBox(
              height: 40,
            ),
            AnimatedCrossFade(
                firstChild: Container(
                  child: Column(
                    children: [
                      Text(
                        "\$ " + usd,
                        style: GoogleFonts.roboto(
                          fontSize: width / 10,
                          color: Colors.lime.shade900,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        "≈",
                        style: GoogleFonts.roboto(
                          fontSize: width / 15,
                          color: Colors.white70,
                        ),
                      ),
                      Text(
                        "LKR " + lkr,
                        style: GoogleFonts.roboto(
                          fontSize: width / 10,
                          color: Colors.lime.shade900,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        "Price for 1 kilogram of Cinnamon in \nSri Lankan Rupees (LKR)",
                        textAlign: TextAlign.center,
                        style: GoogleFonts.roboto(
                          color: Colors.white70,
                          fontSize: width / 19,
                        ),
                      ),
                    ],
                  ),
                ),
                secondChild: Text("Loading...."),
                crossFadeState: showPrediction
                    ? CrossFadeState.showFirst
                    : CrossFadeState.showSecond,
                duration: Duration(milliseconds: 500))
          ],
        ),
      ),
    );
  }
}
