import 'package:apps_sidang/generated/assets.dart';
import 'package:apps_sidang/src/prediction/provider/predict_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animated_button/flutter_animated_button.dart';
// import 'package:flutter_flip_card/flutter_flip_card.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';
// import 'package:ripple_wave/ripple_wave.dart';

class PredictView extends StatefulWidget {
  final double height;
  final String age;
  final String gender;
  final String breastfeeding;
  const PredictView({
    super.key,
    required this.height,
    required this.age,
    required this.gender,
    required this.breastfeeding,
  });

  @override
  State<PredictView> createState() => _PredictViewState();
}

class _PredictViewState extends State<PredictView> {
  @override
  void initState() {
    getData();
    super.initState();
  }

  getData() {
    context.read<PredictProvider>().fetchPredict(
      height: widget.height,
      age: widget.age,
      gender: widget.gender,
      breastfeeding: widget.breastfeeding,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          "Prediksi",
          style: TextStyle(fontWeight: FontWeight.w500, fontFamily: 'FiraCode'),
        ),
      ),
      body: Consumer<PredictProvider>(
        builder: (context, predictP, _) {
          return SingleChildScrollView(
            child: SizedBox(
              height:
                  predictP.withLoading == true
                      ? MediaQuery.of(context).size.height - kToolbarHeight
                      : null,
              child: Column(
                mainAxisAlignment:
                    predictP.withLoading == true
                        ? MainAxisAlignment.center
                        : MainAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  predictP.withLoading == true ? loadingPredict() : SizedBox(),
                  predictP.withLoading == false ? resultPredict() : SizedBox(),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  Widget loadingPredict() {
    return Column(
      children: [
        Lottie.asset(Assets.assetsImagesThink),
        SizedBox(height: 10),
        Text(
          "Tunggu Sebentar",
          style: TextStyle(fontWeight: FontWeight.w500, fontFamily: 'FiraCode'),
        ),
      ],
    );
  }

  Widget resultPredict() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: SizedBox(
        width: double.infinity,
        height: MediaQuery.of(context).size.height * 0.85,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: 20),
            cardCustom(),
            SizedBox(height: 30),
            cardDesc1(),
            SizedBox(height: 20),
            cardDesc2(),
            Spacer(),
            AnimatedButton(
              onPress: () async {
                // Variabel kontak dan pesan
                String contact = "+6285791643208";
                String text = 'Hallo Saya Ingin Berkonsultasi';

                // Encode teks untuk URL
                String encodedText = Uri.encodeComponent(text);

                // Buat URL WhatsApp
                String whatsappUrl =
                    "https://api.whatsapp.com/send?phone=$contact&text=$encodedText";

                // Cek apakah URL dapat diluncurkan
                if (await canLaunchUrl(Uri.parse(whatsappUrl))) {
                  await launchUrl(Uri.parse(whatsappUrl));
                } else {
                  throw 'Could not launch WhatsApp. Please check your device configuration.';
                }
              },
              height: 45,
              width: double.infinity,
              text: 'KONSULTASI',
              isReverse: true,
              selectedTextColor: Colors.white,
              transitionType: TransitionType.CENTER_ROUNDER,
              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,
            ),
          ],
        ),
      ),
    );
  }

  Widget cardCustom() {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black,
            blurStyle: BlurStyle.solid,
            offset: Offset(0, 5),
            blurRadius: 0,
          ),
        ],
      ),
      child: Stack(
        children: [
          Container(
            width: double.infinity,
            height: 173,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
              border: Border.all(color: Colors.black, width: 1),
              boxShadow: [
                BoxShadow(
                  color: Colors.black,
                  blurStyle: BlurStyle.solid,
                  offset: Offset(0, 5),
                  blurRadius: 0,
                ),
              ],
            ),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: [
                  Text(
                    "Status Stunting",
                    style: TextStyle(
                      fontWeight: FontWeight.w500,
                      fontFamily: 'FiraCode',
                    ),
                  ),
                  SizedBox(height: 3),
                  Consumer<PredictProvider>(
                    builder: (context, predictP, _) {
                      return Text(
                        '"${predictP.predictModel.result == 'N'
                            ? 'Tidak'
                            : predictP.predictModel.result == 'Y'
                            ? 'Iya'
                            : '-'}"',
                        style: TextStyle(
                          fontWeight: FontWeight.w700,
                          fontFamily: 'FiraCode',
                          fontStyle: FontStyle.italic,
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          ),

          Positioned(
            bottom: 0,
            right: 0,
            child: Image.asset(Assets.assetsImagesCat1, width: 100),
          ),
          Positioned(
            top: 0,
            left: -13,
            child: Image.asset(Assets.assetsImagesMonkey1, width: 90),
          ),
        ],
      ),
    );
  }

  Widget cardDesc1() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Colors.black, width: 1),
        boxShadow: [
          BoxShadow(
            color: Colors.black,
            blurStyle: BlurStyle.solid,
            offset: Offset(0, 5),
            blurRadius: 0,
          ),
        ],
      ),
      child: Center(
        child: Text(
          "Cara Mencegah Stunting",
          style: TextStyle(fontWeight: FontWeight.w500, fontFamily: 'FiraCode'),
        ),
      ),
    );
  }

  Widget cardDesc2() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Colors.black, width: 1),
        boxShadow: [
          BoxShadow(
            color: Colors.black,
            blurStyle: BlurStyle.solid,
            offset: Offset(0, 5),
            blurRadius: 0,
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "1. Aktif minum Tablet Tambah Darah (TTD)",
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontFamily: 'FiraCode',
            ),
          ),
          Text(
            "2. Bumil teratur periksa kehamilan minimal 6 kali",
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontFamily: 'FiraCode',
            ),
          ),
          Text(
            "3. Cukupi konsumsi protein hewani",
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontFamily: 'FiraCode',
            ),
          ),
          Text(
            "4. Datang ke Posyandu setiap bulan",
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontFamily: 'FiraCode',
            ),
          ),
          Text(
            "5. Eksklusif ASI selama 6 bulan",
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontFamily: 'FiraCode',
            ),
          ),
          SizedBox(height: 15),
          SizedBox(
            width: double.infinity,
            child: Center(
              child: Text(
                '"Kementrian Kesehatan"',
                style: TextStyle(
                  fontWeight: FontWeight.w700,
                  fontFamily: 'FiraCode',
                  fontSize: 12,
                  fontStyle: FontStyle.italic,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
