import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:garage/utils/color_utils.dart';
import 'package:provider/provider.dart';

import '../../car/widget/date_picker_button.dart';
import '../model/user_provider.dart';

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

  @override
  State<UserInfoTile> createState() => _UserInfoTileState();
}

class _UserInfoTileState extends State<UserInfoTile> {
  bool editing = false;
  DateTime? licenceExpiration;

  @override
  Widget build(BuildContext context) {
    final provider = context.watch<UserProvider>();
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: Container(
        height: 178,
        width: double.infinity,
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(20)),
          boxShadow: [
            BoxShadow(
                color: Colors.grey,
                spreadRadius: 0,
                blurRadius: 30,
                offset: Offset(0, 7.5)),
          ],
        ),
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            children: [
              Row(
                children: [
                  Container(
                    width: 100,
                    height: 100,
                    decoration: const BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(100)),
                    ),
                    child: ClipRRect(
                        borderRadius: BorderRadius.circular(20),
                        child: Image.asset("images/unknown.jpg")),
                  ),
                  Expanded(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text('${FirebaseAuth.instance.currentUser!.email}',
                            style: TextStyle(fontSize: 25)),
                      ],
                    ),
                  )
                ],
              ),
              Spacer(),
              Row(
                children: [
                  Text(
                    "Expirace řidičského průkazu:",
                    style: TextStyle(fontSize: 16),
                  ),
                  editing
                      ? DatePickerButton(
                          date: provider.currentUser!.licenceExpiration,
                          onSave: (date) =>
                              setState(() => licenceExpiration = date))
                      : Padding(
                          padding: const EdgeInsets.only(left: 12.0),
                          child: provider.currentUser!.licenceExpiration != null
                              ? getStyledDateAsText(
                                  context,
                                  provider.currentUser!.licenceExpiration!,
                                )
                              : Text(
                                  "Datum nezadán",
                                  style: TextStyle(fontStyle: FontStyle.italic),
                                ),
                        ),
                  Expanded(
                      child: editing
                          ? IconButton(
                              onPressed: () => setState(() {
                                    provider.setLicenceExpiration(
                                        licenceExpiration);
                                    editing = false;
                                  }),
                              icon: Icon(Icons.done, size: 17))
                          : IconButton(
                              onPressed: () => setState(() {
                                    licenceExpiration =
                                        provider.currentUser!.licenceExpiration;
                                    editing = true;
                                  }),
                              icon: Icon(Icons.edit, size: 17))),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
