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

/// Widget untuk dialog konfigurasi teks kustom (disclaimer, thank you, pantun)
class CustomTextConfigDialog extends StatefulWidget {
  const CustomTextConfigDialog({super.key});

  @override
  State<CustomTextConfigDialog> createState() => _CustomTextConfigDialogState();
}

class _CustomTextConfigDialogState extends State<CustomTextConfigDialog> {
  final _formKey = GlobalKey<FormState>();
  
  // Controller untuk teks kustom
  final TextEditingController _disclaimerController = TextEditingController();
  final TextEditingController _thankYouController = TextEditingController();
  final TextEditingController _pantunController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _loadCustomTexts();
  }

  /// Memuat teks kustom dari shared preferences
  Future<void> _loadCustomTexts() async {
    final prefs = await SharedPreferences.getInstance();
    
    setState(() {
      _disclaimerController.text = prefs.getString('store_disclaimer_text') ?? 
          'Barang yang sudah dibeli tidak dapat dikembalikan/ditukar. '
          'Harap periksa kembali struk belanja Anda sebelum meninggalkan toko.';
      
      _thankYouController.text = prefs.getString('thank_you_text') ?? '*** TERIMA KASIH ***';
      
      _pantunController.text = prefs.getString('pantun_text') ?? 
          'Belanja di toko kami, hemat dan nyaman,\\n'
          'Dengan penuh semangat, kami siap melayani,\\n'
          'Harapan kami, Anda selalu puas,\\n'
          'Sampai jumpa lagi, selamat tinggal.';
    });
  }

  /// Menyimpan teks kustom ke shared preferences
  Future<void> _saveCustomTexts() async {
    if (_formKey.currentState!.validate()) {
      final prefs = await SharedPreferences.getInstance();
      
      await prefs.setString('store_disclaimer_text', _disclaimerController.text);
      await prefs.setString('thank_you_text', _thankYouController.text);
      await prefs.setString('pantun_text', _pantunController.text);
      
      if (mounted) {
        Navigator.of(context).pop(true); // Kembali dengan nilai true jika berhasil disimpan
      }
    }
  }

  @override
  void dispose() {
    _disclaimerController.dispose();
    _thankYouController.dispose();
    _pantunController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('Konfigurasi Teks Kustom'),
      content: Form(
        key: _formKey,
        child: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextFormField(
                controller: _disclaimerController,
                decoration: const InputDecoration(
                  labelText: 'Disclaimer Toko',
                  border: OutlineInputBorder(),
                  alignLabelWithHint: true,
                ),
                maxLines: 3,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Mohon masukkan teks disclaimer';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 16),
              TextFormField(
                controller: _thankYouController,
                decoration: const InputDecoration(
                  labelText: 'Ucapan Terima Kasih',
                  border: OutlineInputBorder(),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Mohon masukkan ucapan terima kasih';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 16),
              TextFormField(
                controller: _pantunController,
                decoration: const InputDecoration(
                  labelText: 'Pantun/Ucapan',
                  border: OutlineInputBorder(),
                  alignLabelWithHint: true,
                ),
                maxLines: 4,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Mohon masukkan pantun/ucapan';
                  }
                  return null;
                },
              ),
            ],
          ),
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(false), // Kembali dengan nilai false jika dibatalkan
          child: const Text('Batal'),
        ),
        ElevatedButton(
          onPressed: _saveCustomTexts,
          child: const Text('Simpan'),
        ),
      ],
    );
  }
}