import 'package:anyway/structs/agreement.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';

import 'package:anyway/modules/onboarding_card.dart';


class OnboardingAgreementCard extends StatefulWidget {
  final String title;
  final String description;
  final String imagePath;
  final String agreementTextPath;
  final ValueChanged<bool> onAgreementChanged;


  OnboardingAgreementCard({
    super.key, 
    required this.title,
    required this.description,
    required this.imagePath,
    required this.agreementTextPath,
    required this.onAgreementChanged
  });

  @override
  State<OnboardingAgreementCard> createState() => _OnboardingAgreementCardState();
}

class _OnboardingAgreementCardState extends State<OnboardingAgreementCard> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(20),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          OnboardingCard(title: widget.title, description: widget.description, imagePath: widget.imagePath),
          Padding(padding: EdgeInsets.only(top: 20)),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // The checkbox of the agreement
              FutureBuilder(
                future: getAgreement(),
                builder: (context, snapshot) {
                  bool agreed = false;
                  if (snapshot.connectionState == ConnectionState.done) {
                    if (snapshot.hasData) {
                      Agreement agreement = snapshot.data!;
                      agreed = agreement.agreed;
                    } else {
                      agreed = false;
                    }
                  } else {
                    agreed = false;
                  }
                  return Checkbox(
                    value: agreed,
                    onChanged: (value) {
                      setState(() {
                        widget.onAgreementChanged(value!);
                      });
                      saveAgreement(value!);
                    },
                  );
                },
              ),
              
              // The text of the agreement
              Text(
                "I agree to the ",
                style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                  color: Colors.white,
                ),
              ),
              
              // The clickable text of the agreement that shows the agreement text
              GestureDetector(
                onTap: () {
                  // show a dialog with the agreement text
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        scrollable: true,
                        content: FutureBuilder(
                          future: DefaultAssetBundle.of(context).loadString(widget.agreementTextPath),
                          builder: (context, snapshot) {
                            if (snapshot.hasData) {
                              return MarkdownBody(
                                data: snapshot.data.toString(),
                              );
                            } else {
                              return CircularProgressIndicator();
                            }
                            
                          },
                        )
                      );
                    }
                  );

                },
                child: Text(
                  "Terms of Service (click to view)",
                  style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}