import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

import '../../../../../common/theme/theme.dart';
import 'widgets/order_card.dart';

// Model untuk Order
class Order {
  final String id;
  final String customerName;
  final DateTime orderDate;
  final List<OrderItem> items;
  final double totalAmount;
  final OrderStatus status;
  final String? notes;
  final String? phoneNumber;
  final String? address;

  Order({
    required this.id,
    required this.customerName,
    required this.orderDate,
    required this.items,
    required this.totalAmount,
    required this.status,
    this.notes,
    this.phoneNumber,
    this.address,
  });
}

class OrderItem {
  final String name;
  final int quantity;
  final double price;
  final String? imageUrl;
  final String? notes;

  OrderItem({
    required this.name,
    required this.quantity,
    required this.price,
    this.imageUrl,
    this.notes,
  });
}

enum OrderStatus { pending, processing, completed, cancelled }

@RoutePage()
class OrderPage extends StatefulWidget {
  const OrderPage({super.key});

  @override
  State<OrderPage> createState() => _OrderPageState();
}

class _OrderPageState extends State<OrderPage> with TickerProviderStateMixin {
  late TabController _tabController;
  bool _isLoading = true;
  List<Order> _orders = [];

  // Filter states

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 5, vsync: this);
    _loadOrders();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void _loadOrders() {
    // Simulate loading
    Future.delayed(const Duration(seconds: 2), () {
      setState(() {
        _isLoading = false;
        // Uncomment untuk testing dengan data
        _orders = _generateSampleOrders();
      });
    });
  }

  List<Order> _generateSampleOrders() {
    return [
      Order(
        id: "ORD-001",
        customerName: "John Doe",
        orderDate: DateTime.now().subtract(const Duration(hours: 2)),
        address: "Jl. Malioboro No. 123, Yogyakarta",
        items: [
          OrderItem(
            name: "Nasi Gudeg",
            quantity: 2,
            price: 25000,
            notes: "Pedas sedang",
          ),
          OrderItem(name: "Es Teh Manis", quantity: 2, price: 8000),
          OrderItem(name: "Kerupuk", quantity: 1, price: 5000),
        ],
        totalAmount: 71000,
        status: OrderStatus.pending,
        notes: "Tolong diantar sebelum jam 2 siang",
      ),
      Order(
        id: "ORD-002",
        customerName: "Jane Smith",
        orderDate: DateTime.now().subtract(const Duration(hours: 1)),
        address: "Jl. Sultan Agung No. 45, Yogyakarta",
        items: [
          OrderItem(
            name: "Ayam Bakar",
            quantity: 1,
            price: 35000,
            notes: "Tidak pedas",
          ),
          OrderItem(name: "Nasi Putih", quantity: 1, price: 5000),
          OrderItem(name: "Lalapan", quantity: 1, price: 8000),
        ],
        totalAmount: 48000,
        status: OrderStatus.processing,
      ),
      Order(
        id: "ORD-003",
        customerName: "Bob Wilson",
        orderDate: DateTime.now().subtract(const Duration(minutes: 30)),
        phoneNumber: "+62 811-2345-6789",
        items: [
          OrderItem(name: "Gado-gado", quantity: 2, price: 20000),
          OrderItem(name: "Lontong", quantity: 2, price: 3000),
          OrderItem(name: "Es Jeruk", quantity: 2, price: 10000),
        ],
        totalAmount: 66000,
        status: OrderStatus.completed,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(),
      body: Column(
        children: [
          _buildTabBar(),
          Expanded(
            child: _isLoading ? _buildLoadingState() : _buildOrderContent(),
          ),
        ],
      ),
    );
  }

  PreferredSizeWidget _buildAppBar() {
    return AppBar(
      elevation: 0,
      backgroundColor: AppColor.white,
      automaticallyImplyLeading: false,
      title: Text('Pesanan'),
      actions: [
        IconButton(
          onPressed: _showFilterDialog,
          icon: const Icon(Icons.filter_list, color: AppColor.textSecondary),
        ),
        IconButton(
          onPressed: _refreshOrders,
          icon: const Icon(Icons.refresh, color: AppColor.textSecondary),
        ),
      ],
    );
  }

  Widget _buildTabBar() {
    return Container(
      color: AppColor.white,
      child: TabBar(
        controller: _tabController,
        isScrollable: true,
        labelColor: AppColor.primary,
        unselectedLabelColor: AppColor.textSecondary,
        indicatorColor: AppColor.primary,
        indicatorWeight: 3,
        labelStyle: AppStyle.md.copyWith(fontWeight: FontWeight.w600),
        tabAlignment: TabAlignment.start,
        unselectedLabelStyle: AppStyle.md,
        tabs: const [
          Tab(text: 'Semua'),
          Tab(text: 'Menunggu'),
          Tab(text: 'Diproses'),
          Tab(text: 'Selesai'),
          Tab(text: 'Dibatalkan'),
        ],
      ),
    );
  }

  Widget _buildOrderContent() {
    if (_orders.isEmpty) {
      return _buildEmptyState();
    }

    return TabBarView(
      controller: _tabController,
      children: [
        _buildOrderList(_orders),
        _buildOrderList(
          _orders.where((o) => o.status == OrderStatus.pending).toList(),
        ),
        _buildOrderList(
          _orders.where((o) => o.status == OrderStatus.processing).toList(),
        ),
        _buildOrderList(
          _orders.where((o) => o.status == OrderStatus.completed).toList(),
        ),
        _buildOrderList(
          _orders.where((o) => o.status == OrderStatus.cancelled).toList(),
        ),
      ],
    );
  }

  Widget _buildOrderList(List<Order> orders) {
    if (orders.isEmpty) {
      return _buildEmptyState();
    }

    return RefreshIndicator(
      onRefresh: _refreshOrders,
      color: AppColor.primary,
      child: ListView.builder(
        padding: const EdgeInsets.all(16),
        itemCount: orders.length,
        itemBuilder: (context, index) {
          return OrderCard(order: orders[index]);
        },
      ),
    );
  }

  Widget _buildEmptyState() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 120,
            height: 120,
            decoration: BoxDecoration(
              color: AppColor.primary.withOpacity(0.1),
              borderRadius: BorderRadius.circular(60),
            ),
            child: Icon(
              Icons.receipt_long,
              size: 60,
              color: AppColor.primary.withOpacity(0.5),
            ),
          ),
          const SizedBox(height: 24),
          Text(
            'Belum Ada Pesanan',
            style: AppStyle.h6.copyWith(
              fontWeight: FontWeight.bold,
              color: AppColor.textPrimary,
            ),
          ),
          const SizedBox(height: 8),
          Text(
            'Pesanan akan muncul di sini setelah\npelanggan mulai memesan.',
            style: AppStyle.md.copyWith(color: AppColor.textSecondary),
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 32),
          ElevatedButton.icon(
            onPressed: _refreshOrders,
            icon: const Icon(Icons.refresh, size: 20),
            label: const Text('Muat Ulang'),
            style: ElevatedButton.styleFrom(
              backgroundColor: AppColor.primary,
              foregroundColor: AppColor.white,
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildLoadingState() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(AppColor.primary),
          ),
          const SizedBox(height: 16),
          Text(
            'Memuat pesanan...',
            style: AppStyle.md.copyWith(color: AppColor.textSecondary),
          ),
        ],
      ),
    );
  }

  void _showFilterDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(
            'Filter Pesanan',
            style: AppStyle.lg.copyWith(fontWeight: FontWeight.bold),
          ),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // Add filter options here
              Text('Opsi filter akan segera hadir...', style: AppStyle.md),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text(
                'Tutup',
                style: AppStyle.md.copyWith(color: AppColor.primary),
              ),
            ),
          ],
        );
      },
    );
  }

  Future<void> _refreshOrders() async {
    setState(() {
      _isLoading = true;
    });
    await Future.delayed(const Duration(seconds: 1));
    setState(() {
      _isLoading = false;
      // Uncomment untuk testing dengan data
      _orders = _generateSampleOrders();
    });
  }
}
