import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../app/constant.dart';
import 'iframe_view.dart';

class CustomDialogBuilder {
  // // pdf mobile
  // static reportPdf(
  //   BuildContext context,
  //   String typeReportParam,
  //   String startDateParam,
  //   String endDateParam,
  //   String usernameParam,
  //   String filterRegionalID,
  //   String tatReportCategoryCode,
  // ) {
  //   String urlParam = "";
  //   String tatReportCategoryCodeLowerCase = tatReportCategoryCode.toLowerCase();
  //   if (typeReportParam == "type_report_pendapatan") {
  //     // pendapatan
  //     // urlParam =
  //     //     "https://${Constant.ipAddress}/birt/run?__report=report/one/rekap/rpt_sales_001.rptdesign&__format=xlsx&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&tm=${DateTime.now()}";

  //     // urlParam =
  //     //     "https://devone.aplikasi.web.id/birt/frameset?__report=report/one/lab/rpt_test.rptdesign&__format=xls&username=ADMIN&PID=131878&tm=1697596972372";

  //     if (filterRegionalID == "0") {
  //       urlParam =
  //           "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_sales_nasional_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //     } else {
  //       urlParam =
  //           "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_sales_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //     }
  //   } else {
  //     // piutang
  //     if (typeReportParam == "type_report_piutang") {
  //       // urlParam =
  //       //     "https://${Constant.ipAddress}/birt/run?__report=report/one/rekap/rpt_sales_001.rptdesign&__format=xlsx&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&tm=${DateTime.now()}";

  //       // urlParam =
  //       //     "https://${Constant.ipAddress}/birt/frameset?__report=report/one/lab/rpt_test.rptdesign&__format=xls&username=ADMIN&PID=131878&tm=1697596972372";

  //       if (filterRegionalID == "0") {
  //         urlParam =
  //             "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_piutang_nasional_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //       } else {
  //         urlParam =
  //             "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_piutang_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //       }
  //     } else {
  //       // tat
  //       if (filterRegionalID == "0") {
  //         // global
  //         if (tatReportCategoryCodeLowerCase == "global") {
  //           urlParam =
  //               "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //           print("global nasional tat : $urlParam");
  //         } else {
  //           // fo
  //           if (tatReportCategoryCodeLowerCase == "fo") {
  //             urlParam =
  //                 "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //             print("fo nasional tat : $urlParam");
  //           } else {
  //             // proses
  //             if (tatReportCategoryCodeLowerCase == "proses") {
  //               urlParam =
  //                   "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //               print("proses nasional tat : $urlParam");
  //             } else {
  //               // sampling
  //               if (tatReportCategoryCodeLowerCase == "sampling") {
  //                 urlParam =
  //                     "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //                 print("sampling nasional tat : $urlParam");
  //               } else {
  //                 // pengolahan
  //                 if (tatReportCategoryCodeLowerCase == "pengolahan") {
  //                   urlParam =
  //                       "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //                   print("sampling nasional tat : $urlParam");
  //                 } else {
  //                   // validasi
  //                   if (tatReportCategoryCodeLowerCase == "validasi") {
  //                     urlParam =
  //                         "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=pdf ";
  //                     print("sampling nasional tat : $urlParam");
  //                   }
  //                 }
  //               }
  //             }
  //           }
  //         }
  //       } else {
  //         // global
  //         if (tatReportCategoryCodeLowerCase == "global") {
  //           urlParam =
  //               "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //           print("global regional tat : $urlParam");
  //         } else {
  //           // fo
  //           if (tatReportCategoryCodeLowerCase == "fo") {
  //             urlParam =
  //                 "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //             print("fo regional tat : $urlParam");
  //           } else {
  //             // proses
  //             if (tatReportCategoryCodeLowerCase == "proses") {
  //               urlParam =
  //                   "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //               print("proses regional tat : $urlParam");
  //             } else {
  //               // sampling
  //               if (tatReportCategoryCodeLowerCase == "sampling") {
  //                 urlParam =
  //                     "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //                 print("sampling regional tat : $urlParam");
  //               } else {
  //                 // pengolahan
  //                 if (tatReportCategoryCodeLowerCase == "pengolahan") {
  //                   urlParam =
  //                       "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //                   print("sampling regional tat : $urlParam");
  //                 } else {
  //                   // validasi
  //                   if (tatReportCategoryCodeLowerCase == "validasi") {
  //                     urlParam =
  //                         "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=pdf";
  //                     print("sampling regional tat : $urlParam");
  //                   }
  //                 }
  //               }
  //             }
  //           }
  //         }
  //       }
  //     }
  //   }

  //   // launchUrlString(urlParam, mode: LaunchMode.externalNonBrowserApplication);
  //   launchUrlString(urlParam);
  // }

  // // excel web or mobile
  // static reportExcel(
  //   String typeReportParam,
  //   String startDateParam,
  //   String endDateParam,
  //   String usernameParam,
  //   String filterRegionalID,
  //   String tatReportCategoryCode,
  // ) {
  //   String urlParam = "";
  //   String tatReportCategoryCodeLowerCase = tatReportCategoryCode.toLowerCase();
  //   if (typeReportParam == "type_report_pendapatan") {
  //     // pendapatan
  //     // urlParam =
  //     //     "https://${Constant.ipAddress}/birt/run?__report=report/one/rekap/rpt_sales_001.rptdesign&__format=xlsx&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&tm=${DateTime.now()}";

  //     // urlParam =
  //     //     "https://devone.aplikasi.web.id/birt/frameset?__report=report/one/lab/rpt_test.rptdesign&__format=xls&username=ADMIN&PID=131878&tm=1697596972372";

  //     if (filterRegionalID == "0") {
  //       urlParam =
  //           "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_sales_nasional_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //     } else {
  //       urlParam =
  //           "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_sales_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //     }
  //   } else {
  //     if (typeReportParam == "type_report_piutang") {
  //       // piutang
  //       // urlParam =
  //       //     "https://${Constant.ipAddress}/birt/run?__report=report/one/rekap/rpt_sales_001.rptdesign&__format=xlsx&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&tm=${DateTime.now()}";

  //       // urlParam =
  //       //     "https://${Constant.ipAddress}/birt/frameset?__report=report/one/lab/rpt_test.rptdesign&__format=xls&username=ADMIN&PID=131878&tm=1697596972372";

  //       if (filterRegionalID == "0") {
  //         urlParam =
  //             "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_piutang_nasional_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //       } else {
  //         urlParam =
  //             "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_piutang_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //       }
  //     } else {
  //       // tat
  //       // if (filterRegionalID == "0") {
  //       //   urlParam =
  //       //       "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //       // } else {
  //       //   urlParam =
  //       //       "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //       // }

  //       // tat
  //       if (filterRegionalID == "0") {
  //         // global
  //         if (tatReportCategoryCodeLowerCase == "global") {
  //           urlParam =
  //               "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //           print("global nasional tat : $urlParam");
  //         } else {
  //           // fo
  //           if (tatReportCategoryCodeLowerCase == "fo") {
  //             urlParam =
  //                 "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //             print("fo nasional tat : $urlParam");
  //           } else {
  //             // proses
  //             if (tatReportCategoryCodeLowerCase == "proses") {
  //               urlParam =
  //                   "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //               print("proses nasional tat : $urlParam");
  //             } else {
  //               // sampling
  //               if (tatReportCategoryCodeLowerCase == "sampling") {
  //                 urlParam =
  //                     "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //                 print("sampling nasional tat : $urlParam");
  //               } else {
  //                 // pengolahan
  //                 if (tatReportCategoryCodeLowerCase == "pengolahan") {
  //                   urlParam =
  //                       "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //                   print("sampling nasional tat : $urlParam");
  //                 } else {
  //                   // validasi
  //                   if (tatReportCategoryCodeLowerCase == "validasi") {
  //                     urlParam =
  //                         "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&__format=xls ";
  //                     print("sampling nasional tat : $urlParam");
  //                   }
  //                 }
  //               }
  //             }
  //           }
  //         }
  //       } else {
  //         // global
  //         if (tatReportCategoryCodeLowerCase == "global") {
  //           urlParam =
  //               "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //           print("global regional tat : $urlParam");
  //         } else {
  //           // fo
  //           if (tatReportCategoryCodeLowerCase == "fo") {
  //             urlParam =
  //                 "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //             print("fo regional tat : $urlParam");
  //           } else {
  //             // proses
  //             if (tatReportCategoryCodeLowerCase == "proses") {
  //               urlParam =
  //                   "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //               print("proses regional tat : $urlParam");
  //             } else {
  //               // sampling
  //               if (tatReportCategoryCodeLowerCase == "sampling") {
  //                 urlParam =
  //                     "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //                 print("sampling regional tat : $urlParam");
  //               } else {
  //                 // pengolahan
  //                 if (tatReportCategoryCodeLowerCase == "pengolahan") {
  //                   urlParam =
  //                       "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //                   print("sampling regional tat : $urlParam");
  //                 } else {
  //                   // validasi
  //                   if (tatReportCategoryCodeLowerCase == "validasi") {
  //                     urlParam =
  //                         "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_tat_nasional_001i_$tatReportCategoryCodeLowerCase.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&PregionalID=${filterRegionalID}&username=${usernameParam}&__format=xls";
  //                     print("sampling regional tat : $urlParam");
  //                   }
  //                 }
  //               }
  //             }
  //           }
  //         }
  //       }
  //     }
  //   }

  //   launchUrlString(urlParam);
  // }

  // pdf web
  static dialogBuilderWebPDF(
      BuildContext context,
      String startDateParam,
      String endDateParam,
      String noRegNamaParam,
      String username,
      String companyID) {
    String urlParam = "";
    String titleParam = "";

    // pendapatan
    var param = {
      "CompanyID": companyID,
      "startDateParam": startDateParam,
      "endDateParam": endDateParam,
      "noRegNamaParam": noRegNamaParam
    };

    print(param);

    // Parsing tanggal dan waktu
    DateTime dateTime = DateTime.now();

    // Mengonversi ke timestamp
    int timestamp = dateTime.millisecondsSinceEpoch;

    titleParam = "Report Order";
    urlParam =
        "${Constant.protocol}//${Constant.ipAddress}/birt/run?__report=report/one/regional/mitra/rpt_list_order.rptdesign&__format=pdf&PStartDate=$startDateParam&PEndDate=$endDateParam&PSearch=$noRegNamaParam&username=$username&CompanyID=$companyID&tm=$timestamp";
    // urlParam =
    //     "https://${Constant.ipAddress}/birt/run?__report=report/one/rekap/rpt_sales_001.rptdesign&__format=pdf&PStartDate=$startDateParam&PEndDate=$endDateParam&username=${usernameParam}&tm=${DateTime.now()}";

    // urlParam =
    //     "https://${Constant.ipAddress}/birt/run?__report=report/one/one_dash/rpt_sales_nasional_001.rptdesign&PStartDate=$startDateParam&PEndDate=$endDateParam&noreg=${noRegNamaParam}&__format=pdf ";

    // urlParam =
    //     "https://devone.aplikasi.web.id/birt/frameset?__report=report/one/lab/rpt_test.rptdesign&__format=pdf&username=ADMIN&PID=131878&tm=1697596972372";

    return showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return HookConsumer(builder: (context, ref, widget) {
          final isLoading = useState(true);
          useEffect(() {
            Timer(Duration(seconds: 3), () {
              isLoading.value = false;
            });
            return null;
          }, []);

          return AlertDialog(
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
            title: Text(titleParam),
            content: Padding(
              padding: const EdgeInsets.all(10),
              child: SizedBox(
                width: Constant.getActualX(context: context, x: 1440),
                child: Column(
                  children: [
                    // Text('${startDateParam}'),
                    // Text('${endDateParam}'),
                    // Text('${noRegNamaParam}'),
                    Expanded(
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          IframeView(
                            key: UniqueKey(),
                            source: urlParam,
                            onCreated: (idx) {
                              // print('Oncreated print ${idx}');
                              isLoading.value = false;
                            },
                          ),
                          // loading
                          if (isLoading.value == true)
                            const Center(
                              child: SizedBox(
                                width: 32,
                                height: 32,
                                child: CircularProgressIndicator(),
                              ),
                            ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            actions: <Widget>[
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                  textStyle: Constant.body3_600(context: context),
                ),
                onPressed: (isLoading.value)
                    ? null
                    : () {
                        isLoading.value = false;
                        Navigator.of(context).pop();
                      },
                child: Padding(
                  padding: const EdgeInsets.all(10),
                  child: Text(
                    'Tutup',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ],
          );
        });
      },
    );
  }
}
