import 'package:easycook_mobile/blocs/supplier/supplier_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:url_launcher/url_launcher.dart';

class SupplierPage extends StatefulWidget {
  final int recipeId;

  const SupplierPage({super.key, required this.recipeId});
  @override
  State<SupplierPage> createState() => _SupplierPageState();
}

class _SupplierPageState extends State<SupplierPage> {
  late SupplierCubit _fetchSupplier;

  @override
  void initState() {
    _fetchSupplier = SupplierCubit()..supplier(id: widget.recipeId);
    super.initState();
  }

  Future<void> _launchUrl(int id) async {
    final Uri _url = Uri.parse('https://www.panenpanen.id/productdetail/$id');
    if (!await launchUrl(_url)) {
      throw Exception('Could not launch $_url');
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _fetchSupplier.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Rekomendasi Bahan"),
        centerTitle: true,
        foregroundColor: Colors.black,
        backgroundColor: Colors.white,
        elevation: 0.5,
      ),
      body: Container(
        padding: EdgeInsets.all(16),
        child: BlocProvider(
          create: (context) => _fetchSupplier,
          child: BlocBuilder(
            bloc: _fetchSupplier,
            builder: (context, state) {
              return state is SupplierLoading
                  ? Center(
                      child: CircularProgressIndicator(),
                    )
                  : state is SupplierFailure
                      ? Center(
                          child: Text(state.message),
                        )
                      : state is SupplierSuccess
                          ? Container(
                              child: state.supplier.length > 0
                                  ? ListView.separated(
                                      separatorBuilder: (context, index) =>
                                          SizedBox(height: 10),
                                      shrinkWrap: true,
                                      itemCount: state.supplier.length,
                                      itemBuilder:
                                          (BuildContext context, int index) {
                                        final data = state.supplier[index];
                                        return InkWell(
                                          onTap: () {},
                                          child: Container(
                                            decoration: BoxDecoration(
                                              color: Colors.amber,
                                              border: Border.all(
                                                color: Colors.black,
                                                width: 1.0,
                                              ),
                                              borderRadius:
                                                  BorderRadius.circular(10),
                                            ),
                                            padding: EdgeInsets.all(10),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: [
                                                Text(
                                                  "mencoba",
                                                  style:
                                                      GoogleFonts.montserrat()
                                                          .copyWith(
                                                    fontSize: 16,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                SizedBox(
                                                  height: 5,
                                                ),
                                                Row(
                                                  children: [
                                                    Text(
                                                      "Harga: ",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    ),
                                                    Text(
                                                      "Rp. ",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    ),
                                                    Text(
                                                      "lagi coba",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    )
                                                  ],
                                                ),
                                                SizedBox(
                                                  height: 5,
                                                ),
                                                Row(
                                                  children: [
                                                    Text(
                                                      "Jumlah: ",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    ),
                                                    Text(
                                                      "Lorem",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    ),
                                                    SizedBox(
                                                      width: 3,
                                                    ),
                                                    Text(
                                                      "lorem",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    )
                                                  ],
                                                ),
                                                SizedBox(
                                                  height: 5,
                                                ),
                                                Row(
                                                  children: [
                                                    Text(
                                                      "Nama Supplier: ",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    ),
                                                    Text(
                                                      'Coba',
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    )
                                                  ],
                                                ),
                                                SizedBox(
                                                  height: 5,
                                                ),
                                                Row(
                                                  children: [
                                                    Text(
                                                      "Alamat: ",
                                                      style: GoogleFonts
                                                              .montserrat()
                                                          .copyWith(
                                                        fontSize: 14,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    ),
                                                    Expanded(
                                                      child: Text(
                                                        "kanfgkl",
                                                        style: GoogleFonts
                                                                .montserrat()
                                                            .copyWith(
                                                          fontSize: 14,
                                                          fontWeight:
                                                              FontWeight.bold,
                                                        ),
                                                      ),
                                                    )
                                                  ],
                                                ),
                                              ],
                                            ),
                                          ),
                                        );
                                      })
                                  : Center(
                                      child: Text(
                                          "Bahan yang diinginkan tidak tersedia"),
                                    ),
                            )
                          : SizedBox();
            },
          ),
        ),
      ),
    );
  }
}
