import 'dart:io';
import 'package:flutter/material.dart' show Colors;
import 'package:flutter/services.dart';
import 'package:grostore/helpers/common_functions.dart';
import 'package:grostore/screens/order/pdf_api.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/widgets.dart';

import '../../models/order/order_details_response.dart';

class PdfInvoiceApi {
  static Future<File> generate(OrderDetailsInfo? detailsInfo) async {
    final img = await rootBundle.load(getAssetLogo("logo2x2.png"));
    final imageBytes = img.buffer.asUint8List();

    final imgQr = await rootBundle.load(getAssetLogo("img.png"));
    final qr = imgQr.buffer.asUint8List();
    final pdf = Document();

    pdf.addPage(MultiPage(
      build: (context) => [
        buildLogo(imageBytes),
        pw.SizedBox(height: 0.3 * PdfPageFormat.cm),
        buildDottedDivider(),
        pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
        buildAddress(
            "${detailsInfo?.shippingAddress?.address}, ${detailsInfo?.shippingAddress?.cityName}, ${detailsInfo?.shippingAddress?.stateName}, ${detailsInfo?.shippingAddress?.countryName}"),
        pw.SizedBox(height: 0.3 * PdfPageFormat.cm),
        buildPhoneNumber("${detailsInfo?.payment_method}"),
        pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
        buildDottedDivider(),
        pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
        buildCheckDate("${detailsInfo?.code}", "${detailsInfo?.date}"),
        pw.SizedBox(height: 0.3 * PdfPageFormat.cm),
        buildDottedDivider(),
        pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
        buildItemsList(detailsInfo?.items ?? []),
        pw.SizedBox(height: 0.7 * PdfPageFormat.cm),
        buildDottedDivider(),
        pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
        buildSummery(detailsInfo),
        buildDottedDivider(),
        pw.SizedBox(height: 1.2 * PdfPageFormat.cm),
        buildQrCode(qr)
      ],
      //footer: (context) => buildFooter(invoice),
    ));

    return PdfApi.saveDocument(name: 'Check.pdf', pdf: pdf);
  }

  static Widget buildLogo(Uint8List image) {
    return Row(mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, children: [
      Text("Karvon market",
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
          )),
      pw.Image(pw.MemoryImage(image), height: 40)
    ]);
  }

  static Widget buildDottedDivider() {
    return Row(
        children: List.generate(
            800 ~/ 10,
            (index) => index % 2 == 0
                ? Expanded(
                    child: pw.Container(height: 1, color: PdfColor(0, 0, 0)),
                  )
                : Expanded(
                    child: Container(height: 2, color: null),
                  )));
  }

  static Widget buildAddress(String address) {
    return pw.Text(address, style: TextStyle(fontSize: 14));
  }

  static Widget buildPhoneNumber(String phoneNumber) {
    return pw.Text("To'lov turi: $phoneNumber", style: TextStyle(fontSize: 14));
  }

  static Widget buildCheckDate(String checkNumber, String date) {
    return Column(
        mainAxisAlignment: pw.MainAxisAlignment.start,
        crossAxisAlignment: pw.CrossAxisAlignment.start,
        children: [
          Text("Check raqami: $checkNumber", style: TextStyle(fontSize: 14)),
          pw.SizedBox(height: 0.2 * PdfPageFormat.cm),
          Text("Chiqarilgan: $date", style: TextStyle(fontSize: 14))
        ]);
  }

  static Widget buildItemsList(List<Item> items) {
    return Column(
        mainAxisSize: pw.MainAxisSize.min,
        mainAxisAlignment: pw.MainAxisAlignment.start,
        children: List.generate(
          items.length,
          (index) => Column(
              crossAxisAlignment: pw.CrossAxisAlignment.start,
              children: [
                Text("${index + 1}.${items[index].product?.name}",
                    style: TextStyle(
                        fontSize: 16, fontWeight: pw.FontWeight.bold)),
                pw.SizedBox(height: 0.1 * PdfPageFormat.cm),
                Row(
                    mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
                    children: [
                      Text("Narxi:",
                          style: TextStyle(
                              fontSize: 16, fontWeight: pw.FontWeight.normal)),
                      Text(items[index].unitPrice,
                          style: TextStyle(
                              fontSize: 15, fontWeight: pw.FontWeight.normal)),
                      Text("${items[index].qty} ta",
                          style: TextStyle(
                              fontSize: 15, fontWeight: pw.FontWeight.normal)),
                      Text(items[index].totalPrice,
                          style: TextStyle(
                              fontSize: 17, fontWeight: pw.FontWeight.bold)),
                    ])
              ]),
        ));
  }

  static Widget buildSummery(OrderDetailsInfo? info) {
    return Column(
        crossAxisAlignment: pw.CrossAxisAlignment.start,
        mainAxisAlignment: pw.MainAxisAlignment.start,
        children: [
          Text("Mahsulot narxi: ${info?.subTotalAmount}",
              style: TextStyle(fontSize: 15)),
          pw.SizedBox(height: 0.2 * PdfPageFormat.cm),
          Text("Yuk tashish narxi:${info?.totalShippingCost}",
              style: TextStyle(fontSize: 15)),
          pw.SizedBox(height: 0.2 * PdfPageFormat.cm),
          Text("Chegirmasi: ${info?.couponDiscountAmount}",
              style: TextStyle(fontSize: 15)),
          pw.SizedBox(height: 0.2 * PdfPageFormat.cm),
          Text("Jami narxi: ${info?.totalPrice}",
              style: TextStyle(fontSize: 15, fontWeight: pw.FontWeight.bold)),
          pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
        ]);
  }

  static Widget buildQrCode(Uint8List qrcode) {
    return pw.Center(child: pw.Image(pw.MemoryImage(qrcode), height: 200));
  }
}
