import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:intl/intl.dart';
import 'package:prosappco/src/components/photo_view.dart';
import 'package:prosappco/src/components/pop_appbar.dart';
import 'package:prosappco/src/models/setting_model.dart';
import 'package:prosappco/src/screens/professional.dart';
import 'package:prosappco/src/screens/reputation.dart';

import '../models/scores_model.dart';

class ProfessionalInfoScreen extends StatefulWidget {
  Professional professional;

  ProfessionalInfoScreen({super.key, required this.professional});

  @override
  State<ProfessionalInfoScreen> createState() => _ProfessionalInfoScreenState();
}

class _ProfessionalInfoScreenState extends State<ProfessionalInfoScreen> {
  SettingModel? settings;

  String formatCurrency(int number) {
    final formatter =
        NumberFormat.currency(locale: 'es_CO', decimalDigits: 0, symbol: '');
    return '\$${formatter.format(number)}';
  }

  @override
  void initState() {
    super.initState();
    if (settings == null) {
      SettingModel.getSettings().then(
        (SettingModel value) => setState(() {
          settings = value;
        }),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: PopAppbar(
            onPressed: () {
              Navigator.pop(context);
            },
            label: widget.professional.name),
        body: SingleChildScrollView(
          child: Stack(
            children: [
              Column(
                children: [
                  Container(
                    width: double.infinity,
                    height: 140,
                    decoration: BoxDecoration(
                      color: const Color(0xFFD6F4FF),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.5),
                          spreadRadius: 1,
                          blurRadius: 7,
                          offset: const Offset(0, 2),
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 50),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const SizedBox(
                          height: 5,
                        ),
                        Text(
                          widget.professional.name,
                          style: const TextStyle(fontWeight: FontWeight.w500),
                        ),
                        Text(
                          widget.professional.professionName,
                          style: const TextStyle(color: Color(0xFF1688C9)),
                        ),
                        widget.professional.getEspecializaciones().isEmpty
                            ? const SizedBox()
                            : Text(
                                'Especializado/a en ${widget.professional.getEspecializaciones()}',
                                style: const TextStyle(color: Colors.black54),
                              ),
                        Text(
                          widget.professional.cityName,
                        ),
                      ],
                    ),
                  ),
                  widget.professional.ubicacion == 'domicilio'
                      ? Container(
                          margin: const EdgeInsets.only(
                              left: 40, right: 40, top: 20, bottom: 20),
                          padding: const EdgeInsets.symmetric(
                              horizontal: 20, vertical: 15),
                          decoration: BoxDecoration(
                            color: const Color(0xFFD6F4FF),
                            borderRadius: BorderRadius.circular(20),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey.withOpacity(0.5),
                                spreadRadius: 1,
                                blurRadius: 5,
                                offset: const Offset(1, 3),
                              ),
                            ],
                          ),
                          child: const Row(
                            children: [
                              Icon(
                                Icons.error_outline,
                                size: 27,
                                color: Colors.black54,
                              ),
                              SizedBox(width: 15),
                              Text(
                                'Este profesional solo atiende en\nsu dirección de trabajo.',
                                style: TextStyle(
                                    color: Colors.black, fontSize: 13),
                              ),
                            ],
                          ),
                        )
                      : const SizedBox(),
                  const SizedBox(height: 10),
                  settings?.tarifas == true
                      ? RichText(
                          text: TextSpan(
                            children: [
                              const TextSpan(
                                text: 'Tarifa consulta  ',
                                style: TextStyle(
                                  color: Colors.black,
                                ),
                              ),
                              TextSpan(
                                text: formatCurrency(
                                    widget.professional.tarifa ?? 0),
                                style: const TextStyle(
                                  color: Colors.black,
                                  fontSize: 16,
                                  fontWeight: FontWeight.w600,
                                ),
                              ),
                            ],
                          ),
                        )
                      : const SizedBox(),
                  const SizedBox(height: 10),
                  const Text(
                    'Se unió el 02 de abril del 2023',
                    style: TextStyle(fontSize: 12),
                  ),
                  const SizedBox(height: 15),
                  Container(
                    decoration: BoxDecoration(
                      color: const Color(0xFFD6F4FF),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.2),
                          spreadRadius: 3,
                          blurRadius: 5,
                          offset: const Offset(0, 3),
                        ),
                      ],
                    ),
                    child: ListTile(
                      onTap: () {
                        Navigator.of(context).push(
                          CupertinoPageRoute(
                            builder: (BuildContext context) {
                              return const ReputationScreen();
                            },
                          ),
                        );
                      },
                      trailing: const Icon(Icons.keyboard_arrow_right,
                          color: Colors.black),
                      title: const Text(
                        'Reputación',
                        style: TextStyle(color: Colors.black),
                      ),
                      subtitle: Row(
                        children: [
                          RatingBar.builder(
                            initialRating: widget.professional.scores.average,
                            minRating: 1,
                            direction: Axis.horizontal,
                            allowHalfRating: true,
                            itemCount: 5,
                            itemSize: 25,
                            maxRating: 5,
                            itemPadding:
                                const EdgeInsets.symmetric(horizontal: 0),
                            itemBuilder: (context, _) => const Icon(
                              Icons.star,
                              color: Color(0xFF2BA4EC),
                            ),
                            onRatingUpdate: (rating) {},
                            ignoreGestures: true,
                          ),
                          const SizedBox(width: 5),
                          Text(
                              '(${widget.professional.scores.total.toString()})  ${widget.professional.scores.average.toStringAsFixed(1)}'),
                        ],
                      ),
                    ),
                  ),
                  ..._scoresList(widget.professional.scores.details),
                  const SizedBox(
                    height: 10,
                  ),
                ],
              ),
              Container(
                padding: const EdgeInsets.only(
                  top: 110,
                  left: 10,
                ),
                child: ReferencePhoto(
                  ref: widget.professional.professionalRef,
                  size: 100,
                  sizeCircle: 100,
                  sizeIcon: 50,
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  List<Widget> _scoresList(List<ScoreDetailModel> list) {
    return list.map((e) => _scoreItem(e)).toList();
  }

  Widget _scoreItem(ScoreDetailModel scoreDetails) {
    return ListTile(
        onTap: () {},
        leading: ReferencePhoto(
          ref: scoreDetails.avatar,
          size: 50,
          sizeCircle: 50,
          sizeIcon: 35,
        ),
        title: Row(
          children: [
            RatingBar.builder(
              initialRating: scoreDetails.score,
              minRating: 1,
              direction: Axis.horizontal,
              allowHalfRating: true,
              itemCount: 5,
              itemSize: 22,
              maxRating: 5,
              itemPadding: const EdgeInsets.symmetric(horizontal: 0),
              itemBuilder: (context, _) => const Icon(
                Icons.star,
                color: Color(0xFF2BA4EC),
              ),
              onRatingUpdate: (rating) {},
              ignoreGestures: true,
            ),
            Text(
              ' (${scoreDetails.score})',
              style: const TextStyle(color: Colors.black54, fontSize: 13),
            )
          ],
        ),
        subtitle: Row(
          children: [
            Expanded(
              child: Text.rich(
                TextSpan(
                  children: [
                    TextSpan(
                      text: '${scoreDetails.name}, ',
                      style: const TextStyle(fontSize: 15, color: Colors.black),
                    ),
                    TextSpan(
                      text: '"${scoreDetails.comment}"',
                      style: const TextStyle(fontSize: 15, color: Colors.grey),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ));
  }
}
