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

class KelompokSupplierPage extends StatefulWidget {
  final int recipeId;
  const KelompokSupplierPage({super.key, required this.recipeId});
  @override
  State<KelompokSupplierPage> createState() => _KelompokSupplierPageState();
}

class _KelompokSupplierPageState extends State<KelompokSupplierPage> {
  late SupplierCubit _fetchSupplier;

  @override
  void initState() {
    // TODO: implement 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 Supplier"),
        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),
                                      itemCount: state.supplier.length,
                                      shrinkWrap: true,
                                      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(
                                                  data.namaSupplier,
                                                  style:
                                                      GoogleFonts.montserrat()
                                                          .copyWith(
                                                    fontSize: 16,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                SizedBox(
                                                  height: 5,
                                                ),
                                                // Row(
                                                //   children: [
                                                //     Text(
                                                //       "Jumlah Kesesuaian Produk: ",
                                                //       style: GoogleFonts
                                                //               .montserrat()
                                                //           .copyWith(
                                                //               fontSize: 16),
                                                //     ),
                                                //     Text(
                                                //       data.jumlahKesesuaianBahan
                                                //           .toString(),
                                                //       style: GoogleFonts
                                                //               .montserrat()
                                                //           .copyWith(
                                                //         fontSize: 16,
                                                //       ),
                                                //     ),
                                                //   ],
                                                // ),
                                                SizedBox(height: 20),
                                                SingleChildScrollView(
                                                  child: Container(
                                                    height: 200,
                                                    child: ListView.separated(
                                                      separatorBuilder:
                                                          (context, index) =>
                                                              SizedBox(
                                                                  height: 5),
                                                      shrinkWrap: true,
                                                      itemCount:
                                                          data.produk.length,
                                                      itemBuilder:
                                                          (BuildContext context,
                                                              int index) {
                                                        final produk =
                                                            data.produk[index];
                                                        return InkWell(
                                                          onTap: () {
                                                            _launchUrl(produk
                                                                .idProduct);
                                                          },
                                                          child: Container(
                                                            decoration:
                                                                BoxDecoration(
                                                              color:
                                                                  Colors.white,
                                                              border: Border.all(
                                                                  color: Colors
                                                                      .black),
                                                              borderRadius:
                                                                  BorderRadius
                                                                      .circular(
                                                                          10),
                                                            ),
                                                            padding:
                                                                EdgeInsets.all(
                                                                    5),
                                                            child: Column(
                                                              crossAxisAlignment:
                                                                  CrossAxisAlignment
                                                                      .start,
                                                              children: [
                                                                Text(
                                                                  produk
                                                                      .namaBahan,
                                                                  style: GoogleFonts
                                                                          .montserrat()
                                                                      .copyWith(
                                                                    fontSize:
                                                                        12,
                                                                  ),
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    Text(
                                                                      "Jumlah : ",
                                                                      style: GoogleFonts
                                                                              .montserrat()
                                                                          .copyWith(
                                                                        fontSize:
                                                                            12,
                                                                      ),
                                                                    ),
                                                                    Text(
                                                                      produk
                                                                          .berat
                                                                          .toString(),
                                                                      style: GoogleFonts
                                                                              .montserrat()
                                                                          .copyWith(
                                                                        fontSize:
                                                                            12,
                                                                      ),
                                                                    ),
                                                                    Text(
                                                                      produk
                                                                          .satuan,
                                                                      style: GoogleFonts
                                                                              .montserrat()
                                                                          .copyWith(
                                                                        fontSize:
                                                                            12,
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    Text(
                                                                      "Harga : ",
                                                                      style: GoogleFonts
                                                                              .montserrat()
                                                                          .copyWith(
                                                                        fontSize:
                                                                            12,
                                                                      ),
                                                                    ),
                                                                    Text(
                                                                      produk
                                                                          .harga
                                                                          .toString(),
                                                                      style: GoogleFonts
                                                                              .montserrat()
                                                                          .copyWith(
                                                                        fontSize:
                                                                            12,
                                                                      ),
                                                                    ),
                                                                  ],
                                                                )
                                                              ],
                                                            ),
                                                          ),
                                                        );
                                                      },
                                                    ),
                                                  ),
                                                )
                                              ],
                                            ),
                                          ),
                                        );
                                      },
                                    )
                                  : Center(
                                      child:
                                          Text("Tidak Ada Supplier Yang Cocok"),
                                    ),
                            )
                          : SizedBox();
            },
          ),
        ),
      ),
    );
  }
}
