import 'package:flutter/material.dart';

class TncDialog extends StatefulWidget {
  final Future<void> Function() onProceed;

  const TncDialog({Key? key, required this.onProceed}) : super(key: key);

  @override
  _TncDialogState createState() => _TncDialogState();
}

class _TncDialogState extends State<TncDialog> {
  bool _isAgreed = false;
  bool _isLoading = false;
  // --- NEW: ScrollController for the TNC text ---
  final ScrollController _scrollController = ScrollController();

  final String _termsAndConditionsText = """
 Effective Date: November 10, 2025

 These Terms and Conditions ("Terms") govern your access to and use of The Bank mobile banking application (the "App") and the services
 provided through it (the "Services").

 By downloading, installing, accessing, or using the App, you agree to be bound by these Terms and our Privacy Policy. If you do not
 agree to these Terms, you must not download, install, access, or use the App.

 1. Definitions
 - App: Refers to The Bank mobile banking application.
 - Bank/We/Us/Our: Refers to The Bank.
 - User/You/Your: Refers to the individual using the App.
 - Device: Refers to any compatible mobile phone, tablet, or other device on which you install and use the App.
 - Security Credentials: Refers to your username, password, PIN, biometric data (e.g., fingerprint, facial recognition), and any other
 authentication methods used to access the App and Services.

 2. Acceptance of Terms
 Your use of the App constitutes your acceptance of these Terms. We recommend that you print or save a copy of these Terms for your
 records.

 3. License to Use
 We grant you a limited, non-exclusive, non-transferable, revocable license to install and use the App on a Device that you own or
 control, solely for your personal, non-commercial use in connection with your accounts at The Bank. This license does not permit you to
 use the App on any Device that you do not own or control.

 4. User Responsibilities
 You agree to:
 - Use the App only for lawful purposes and in accordance with these Terms.
 - Keep your Device and Security Credentials secure and confidential.
 - Notify us immediately if you suspect any unauthorized use of your Security Credentials or Device, or if your Device is lost or stolen.
 - Ensure that any information you provide to us through the App is accurate and up-to-date.
 - Comply with all reasonable instructions we issue regarding the safe use of your Device and the App.
 - Not use the App in any unlawful manner, for any unlawful purpose, or in any manner inconsistent with these Terms, or act fraudulently
 or maliciously (e.g., by hacking into or inserting malicious code into the App or your Device's operating system).
 - Not download the App from anywhere other than an app store approved by us (e.g., Apple App Store, Google Play Store) or install or use
 it on a jail-broken or rooted device.
 - Delete the App if you change or dispose of a Device that you use to access the Services.

 5. Security
 We employ reasonable security measures to protect your information and transactions conducted through the App. However, you acknowledge
 that no system is entirely secure. You are responsible for maintaining the security of your Device and Security Credentials. We are not
 liable for damages arising from virus contamination in your IT system or if the parameters of your browser are different from the
 required technical conditions.

 6. Privacy
 Your privacy is important to us. Our Privacy Policy explains how we collect, use, and protect your personal information in connection
 with your use of the App and Services. By using the App, you consent to such collection, use, and protection as described in our Privacy
 Policy.

 7. Limitations of Liability and Disclaimer of Warranty
 THE APP AND SERVICES ARE PROVIDED "AS IS" AND "AS AVAILABLE" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
 NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. We do not
 guarantee continuous, uninterrupted, or secure access to any part of our Service, and operation of the App or the Services may be
 interfered with by numerous factors outside of our control.

 IN NO EVENT SHALL WE OR OUR AFFILIATES, LICENSORS, OR CONTRACTORS BE LIABLE FOR ANY CLAIM, ARISING FROM OR RELATED TO THE MOBILE BANKING
 APP OR THE SERVICES, THAT YOU DO NOT STATE IN WRITING IN A COMPLAINT FILED IN A COURT OR ARBITRATION PROCEEDING WITHIN TWO (2) YEARS OF
 THE DATE THAT THE EVENT GIVING RISE TO THE CLAIM OCCURRED. THESE LIMITATIONS WILL APPLY TO ALL CAUSES OF ACTION, WHETHER ARISING FROM
 BREACH OF CONTRACT, TORT (INCLUDING NEGLIGENCE) OR ANY OTHER LEGAL THEORY.

 8. Intellectual Property
 All intellectual property rights in the App and its content (excluding user-generated content) are owned by The Bank or its licensors.
 You are granted a limited license to use the App as set forth in these Terms, but no ownership rights are transferred to you. You must
 not remove or tamper with any copyright notice attached to or contained within the App.

 9. Termination
 We may terminate or suspend your access to the App and Services immediately, without prior notice or liability, for any reason
 whatsoever, including without limitation if you breach these Terms. You may stop using the App at any time. If you wish to deregister
 your digital banking access, you need to notify us.

 10. Changes to Terms
 We reserve the right to modify or replace these Terms at any time. If a revision is material, we will provide at least 30 days' notice
 prior to any new terms taking effect. Your continued use of the App after any such changes constitutes your acceptance of the new Terms.

 11. Governing Law and Dispute Resolution
 These Terms shall be governed and construed in accordance with the laws of your local jurisdiction, without regard to its conflict of
 law provisions. Any dispute arising under these Terms shall be resolved in the courts located in your local jurisdiction.

 12. Contact Information
 If you have any questions about these Terms, please contact us through the channels provided on our official website or within the App.

 13. Electronic Communications
 By using the App, you consent to receive electronic communications from us. These communications may include notices about your account,
 transactional information, and marketing materials.

 14. Third-Party Services
 The App may integrate with or provide links to third-party services. We are not responsible for the content, privacy policies, or
 practices of any third-party websites or services.

 15. Indemnification
 You agree to indemnify and hold harmless The Bank, its affiliates, officers, directors, employees, and agents from any and all claims,
 liabilities, damages, losses, and expenses, including reasonable attorneys' fees, arising out of or in any way connected with your
 access to or use of the App and Services.
 """;

  void _handleProceed() async {
    if (_isLoading) return;

    setState(() {
      _isLoading = true;
    });

    await widget.onProceed();

    if (mounted) {
      setState(() {
        _isLoading = false;
      });
    }
  }

  @override
  void dispose() {
    _scrollController.dispose(); // --- NEW: Dispose the ScrollController ---
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return AlertDialog(
      title: const Text('Terms and Conditions'),
      content: SizedBox(
        height: screenSize.height * 0.5, // 50% of screen height
        width: screenSize.width * 0.9, // 90% of screen width
        // --- MODIFIED: Use a Column to separate scrollable text from fixed checkbox ---
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // --- NEW: Expanded Scrollbar for the TNC text ---
            Expanded(
              child: Scrollbar(
                controller: _scrollController,
                thumbVisibility: true, // Always show the scrollbar thumb
                // To place the scrollbar on the left, you might need to wrap
                // this in a Directionality widget or use a custom scrollbar.
                // For now, it will appear on the right as is standard.
                child: SingleChildScrollView(
                  controller: _scrollController,
                  child: _isLoading
                      ? const Center(
                          child: Padding(
                            padding: EdgeInsets.all(16.0),
                            child: CircularProgressIndicator(),
                          ),
                        )
                      : Text(_termsAndConditionsText),
                ),
              ),
            ),
            const SizedBox(height: 16), // Space between text and checkbox
            // --- MODIFIED: Checkbox Row is now outside the SingleChildScrollView ---
            Row(
              children: [
                Checkbox(
                  value: _isAgreed,
                  onChanged: (bool? value) {
                    setState(() {
                      _isAgreed = value ?? false;
                    });
                  },
                ),
                const Flexible(
                    child: Text('I agree to the Terms and Conditions')),
              ],
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: _isLoading
              ? null
              : () {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text(
                          'You must agree to the terms and conditions to proceed.'),
                      behavior: SnackBarBehavior.floating,
                    ),
                  );
                },
          child: const Text('Disagree'),
        ),
        ElevatedButton(
          onPressed: _isAgreed && !_isLoading ? _handleProceed : null,
          child: const Text('Proceed'),
        ),
      ],
    );
  }
}
