import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mobile_pos/model/print_transaction_model.dart';
import 'package:mobile_pos/model/transition_model.dart';
import 'package:mobile_pos/thermal%20printing%20service/printing_provider/print_thermal_invoice_provider.dart';

import '../GlobalComponents/button_global.dart';
import '../Provider/profile_provider.dart';
import '../constant.dart';

class CustomPrintScreen extends StatefulWidget {
  const CustomPrintScreen({super.key});

  @override
  State<CustomPrintScreen> createState() => _CustomPrintScreenState();
}

class _CustomPrintScreenState extends State<CustomPrintScreen> {
  TextEditingController textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Custom Print'),
      ),
      body: Consumer(builder: (context, ref, __) {
        final printerData = ref.watch(thermalPrinterProvider);
        final personalData = ref.watch(profileDetailsProvider);
        return Padding(
          padding: const EdgeInsets.all(14.0),
          child: Column(
            children: [
              TextFormField(
                controller: textEditingController,
                maxLines: 5,
                decoration: const InputDecoration(
                  floatingLabelBehavior: FloatingLabelBehavior.never,
                  hintText: "Write text here...",
                  border: OutlineInputBorder(),
                ),
              ),
              Spacer(),
              personalData.when(data: (data) {
                return ButtonGlobal(
                  iconWidget: Icons.add,
                  buttontext: "Print",
                  iconColor: Colors.white,
                  buttonDecoration: kButtonDecoration.copyWith(
                      color: kMainColor,
                      borderRadius: const BorderRadius.all(Radius.circular(30))),
                  onPressed: () async {
                    await printerData.printCustomInvoiceNow(
                        transaction: PrintPurchaseTransactionModel(
                            purchaseTransitionModel: PurchaseTransactionModel(
                                customerName: "",
                                customerGst: "",
                                customerType: "",
                                customerAddress: "",
                                customerPhone: "",
                                invoiceNumber: "",
                                purchaseDate: ""),
                            personalInformationModel: data),
                        data: textEditingController.text,
                        context: context);
                  },
                );
              }, error: (e, stack) {
                return Text(e.toString());
              }, loading: () {
                return const CircularProgressIndicator();
              })
            ],
          ),
        );
      }),
    );
  }
}
