import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:intl/intl.dart';
import 'dart:io';

/// Widget untuk menampilkan informasi toko dan admin
/// Dapat di-tap untuk membuka dialog konfigurasi
class StoreInfoWidget extends StatefulWidget {
  final VoidCallback onTap; // Callback ketika widget di-tap

  const StoreInfoWidget({super.key, required this.onTap});

  @override
  State<StoreInfoWidget> createState() => _StoreInfoWidgetState();
}

class _StoreInfoWidgetState extends State<StoreInfoWidget> {
  String storeName = 'TOKO SEMBAKO MURAH';
  String storeAddress = 'Jl. Merdeka No. 123';
  String adminName = 'Budi Santoso';
  String adminPhone = '08123456789';
  String? logoPath; // Path untuk logo toko

  @override
  void initState() {
    super.initState();
    // _loadStoreInfo() will be called in build to ensure it's always up-to-date
  }

  /// Memuat informasi toko dari shared preferences
  Future<void> _loadStoreInfo() async {
    final prefs = await SharedPreferences.getInstance();

    setState(() {
      storeName = prefs.getString('store_name') ?? 'TOKO SEMBAKO MURAH';
      storeAddress = prefs.getString('store_address') ?? 'Jl. Merdeka No. 123';
      adminName = prefs.getString('admin_name') ?? 'Budi Santoso';
      adminPhone = prefs.getString('admin_phone') ?? '08123456789';
      logoPath = prefs.getString('store_logo_path'); // Memuat path logo
    });
  }

  @override
  Widget build(BuildContext context) {
    // Load data every time the widget is built to ensure it's up-to-date
    _loadStoreInfo();

    final courierPrime = GoogleFonts.courierPrime(
      textStyle: const TextStyle(
        fontSize: 14,
        height: 1.2,
      ),
    );

    return GestureDetector(
      onTap: widget.onTap, // Panggil callback ketika di-tap
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.all(8.0),
        color: Colors.white,
        child: Column(
          children: [
            // Menampilkan logo jika tersedia
            if (logoPath != null)
              Container(
                height: 80,
                width: 80,
                margin: const EdgeInsets.only(bottom: 8),
                child: Image.file(
                  File(logoPath!),
                  fit: BoxFit.contain,
                  errorBuilder: (context, error, stackTrace) {
                    // Jika gagal memuat gambar, tampilkan placeholder
                    return Container(
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: const Icon(Icons.image_not_supported, size: 40),
                    );
                  },
                ),
              ),
            Text(
              storeName,
              style: courierPrime.copyWith(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 4),
            Text(storeAddress,
                style: courierPrime, textAlign: TextAlign.center),
            const SizedBox(height: 4),
            Text('Admin: $adminName',
                style: courierPrime, textAlign: TextAlign.center),
            Text('Telp: $adminPhone',
                style: courierPrime, textAlign: TextAlign.center),
            const SizedBox(height: 4),
            Text('Tanggal: ${DateFormat('dd/MM/yyyy').format(DateTime.now())}',
                style: courierPrime, textAlign: TextAlign.center),
          ],
        ),
      ),
    );
  }
}
