import 'package:flutter/material.dart';
import 'package:school_management_system/api/student_requests.dart';
import 'package:school_management_system/components/default_values.dart';
import 'package:school_management_system/screens/profile/components/personal_details.dart';
import 'package:school_management_system/screens/welcome/welcome_screen.dart';

//This Screen Would include the basic Student Detials
//Profile Picture, Full Name, Course of Study, Current Year, Age (DOB), Medical History (Height, Weight, et all)
//Next of Kin, Contact Details, Address
//Every other item as remembered would be added.

late Future<Student> _returnData;
late Student apiData;
late Future<bool> _checkToken;

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({super.key});

  @override
  State<ProfileScreen> createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {
  @override
  void initState() {
    _checkToken = checkTokenValidity();
    super.initState();
    _returnData = getStudentDetail(loginId);
  }

  @override
  Widget build(BuildContext context) {
    _checkToken.then(
      (value) {
        if (!value) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) {
                return const WelcomeScreen();
              },
            ),
          );

          ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(
              content: Text('Session Time Out / Sign Back In'),
            ),
          );
        }
      },
    );
    return FutureBuilder<Student>(
      future: _returnData,
      builder: (context, snapshot) {
        List<Widget> children;
        if (snapshot.hasData) {
          apiData = snapshot.data!;
          children = [
            const SizedBox(height: 20),
            profileImage(),
            const SizedBox(height: 30),
            personalDetails(),
            const SizedBox(height: 30),
            contactDetails(),
            const SizedBox(height: 30),
            medicalDetails(),
            const SizedBox(height: 30),
          ];
        } else if (snapshot.hasError) {
          children = [
            const Icon(
              Icons.error_outline,
              color: Colors.red,
              size: 60,
            ),
            Padding(
              padding: const EdgeInsets.only(top: 16),
              child: Text('Error: ${snapshot.error}'),
            ),
          ];
        } else {
          children = const [
            SizedBox(
              width: 60,
              height: 60,
              child: CircularProgressIndicator(),
            ),
            Padding(
              padding: EdgeInsets.only(top: 16),
              child: Text('Loading Data...'),
            ),
          ];
        }

        return Container(
          color: Colors.white,
          child: Padding(
            padding: const EdgeInsets.only(left: 20, right: 20),
            child: ListView(
              scrollDirection: Axis.vertical,
              children: children,
            ),
          ),
        );
      },
    );
  }

  Column medicalDetails() {
    var medicalDetails = getMedicalDetails(apiData);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Medical Details',
          style: TextStyle(
            color: Colors.black,
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        const SizedBox(height: 5),
        Column(
          children: [
            Table(
              columnWidths: const <int, TableColumnWidth>{
                0: IntrinsicColumnWidth(),
                1: FlexColumnWidth(),
                2: FixedColumnWidth(64)
              },
              children: [
                for (var key in medicalDetails.keys)
                  TableRow(children: [
                    Padding(
                      padding: const EdgeInsets.only(right: 5),
                      child: Text(
                        key,
                        style: const TextStyle(
                            fontSize: 14, fontWeight: FontWeight.w500),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 5),
                      child: Text(medicalDetails[key]),
                    )
                  ])
              ],
            )
          ],
        ),
      ],
    );
  }

  Column contactDetails() {
    var contactDetails = getContactDetails(apiData);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Contact Details',
          style: TextStyle(
            color: Colors.black,
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        const SizedBox(height: 5),
        Column(
          children: [
            Table(
              columnWidths: const <int, TableColumnWidth>{
                0: IntrinsicColumnWidth(),
                1: FlexColumnWidth(),
                2: FixedColumnWidth(64)
              },
              children: [
                for (var key in contactDetails.keys)
                  TableRow(children: [
                    Padding(
                      padding: const EdgeInsets.only(right: 5),
                      child: Text(
                        key,
                        style: const TextStyle(
                            fontSize: 14, fontWeight: FontWeight.w500),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 5),
                      child: Text(
                        contactDetails[key],
                      ),
                    )
                  ])
              ],
            )
          ],
        )
      ],
    );
  }

  Column personalDetails() {
    var personalDetails = getPersonalDetails(apiData);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Personal Details',
          style: TextStyle(
            color: Colors.black,
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        const SizedBox(height: 5),
        Column(
          children: [
            Table(
              // border: TableBorder.all(),
              columnWidths: const <int, TableColumnWidth>{
                0: IntrinsicColumnWidth(),
                1: FlexColumnWidth(),
                2: FixedColumnWidth(64)
              },
              children: [
                for (var key in personalDetails.keys)
                  TableRow(children: [
                    Padding(
                      padding: const EdgeInsets.only(right: 5),
                      child: Text(
                        key,
                        style: const TextStyle(
                            fontSize: 14, fontWeight: FontWeight.w500),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 5),
                      child: Text(personalDetails[key]),
                    )
                  ])
              ],
            )
          ],
        ),
      ],
    );
  }

  Column profileImage() {
    return Column(
      children: [
        Image.asset(
          'assets/images/passport.jpg',
          width: 120,
          height: 120,
        )
      ],
    );
  }
}
