import 'package:drago_pos_printer/utils/esc_pos/generator.dart';
import 'package:flutter/material.dart';
import 'package:max_print_plus/print/bluetooth_printer_screen.dart';
import 'package:max_print_plus/print/network_printer_screen.dart';
import 'package:max_print_plus/print/usb_printer_screen.dart';

import 'tab_bar.dart';

enum HistoryTabData { reservation, dinein, waitinglist, pickup, delivery }

class MaxPrint extends StatefulWidget {
  List<int> bytes;
  EscGenerator generator;
  MaxPrint({Key? key, required this.bytes, required this.generator})
      : super(key: key);

  @override
  State<MaxPrint> createState() => _MaxPrintState();
}

class _MaxPrintState extends State<MaxPrint> with TickerProviderStateMixin {
  String? uid;
  bool? isLogin;
  late final TabController _tabController;
  @override
  void initState() {
    _tabController = TabController(length: 3, vsync: this);
    _tabController.addListener(_handleTabSelection);
    super.initState();
  }

  int tabIndex = 0;

  void _handleTabSelection() {
    setState(() {
      tabIndex = _tabController.index;
      print('Tab index: $tabIndex');
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: DefaultTabController(
      length: 3,
      child: Scaffold(
          backgroundColor: Color(0xFFFAFAFA),
          appBar: AppBar(
            backgroundColor: Color(0xFFFAFAFA),
            centerTitle: true,
            title: const Text(
              'Print',
              style: TextStyle(
                  fontWeight: FontWeight.bold,
                  // letterSpacing: 2.0,
                  fontSize: 16),
            ),
            bottom: CustomTabBar(
              tabController: _tabController,
              tabs: [
                tabTitle('Bluetooth'),
                tabTitle('WiFi'),
                tabTitle('USB'),
                // tabTitle('Ongoing'),
                // tabTitle('Completed'),
              ],
            ),
          ),
          body: Column(
            children: [
              Expanded(
                child: TabBarView(
                  controller: _tabController,
                  children: [
                    BluetoothPrinterScreen(
                        bytes: widget.bytes, generator: widget.generator),
                    NetWorkPrinterScreen(),
                    USBPrinterScreen()
                  ],
                ),
              ),
            ],
          )),
    ));
  }
}

Tab tabTitle(String title) {
  return Tab(
    icon: Text(
      title,
      style: const TextStyle(
          // color: Pallete.primary,
          fontSize: 12.0,
          fontWeight: FontWeight.w600,
          overflow: TextOverflow.ellipsis,
          letterSpacing: 0.1),
      maxLines: 1,
    ),
  );
}
