import 'dart:async';
import 'package:flutter/material.dart';
import 'package:badges/badges.dart' as badges;

import 'package:mobdr/main.dart';
import 'package:mobdr/config/global_style.dart';
import 'package:mobdr/config/constant.dart';
import 'package:mobdr/db/box_photo_typology.dart';
import 'package:mobdr/model/visit_model.dart';
import 'package:mobdr/ui/visit/visit_photo_typology_list.dart';

class VisitPhotoTypologyPage extends StatefulWidget {
  final VisitModel pp_visitModel;

  VisitPhotoTypologyPage({
    required this.pp_visitModel,
  });

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

class _VisitPhotoTypologyPageState extends State<VisitPhotoTypologyPage> {
  GestureDetector Function(BuildContext, int) _itemBuilder(
          List<PhotoTypology> PhotoTypology) =>
      (BuildContext context, int index) => GestureDetector(
            onTap: () {
              Route route = MaterialPageRoute(
                  builder: (context) => VisitPhotoTypologyListPage(
                        pp_visitModel: widget.pp_visitModel,
                        pp_id_typologie:
                            PhotoTypology[index].id_photo_typologie,
                        pp_libelle_typologie: PhotoTypology[index].libelle,
                      ));
              Navigator.push(context, route).then(onGoBack);
            },

            /// TODO objectbox.noteBox.remove(PhotoTypology[index].id),
            child: Container(
              decoration: const BoxDecoration(
                border: Border(bottom: BorderSide(color: Colors.black12)),
              ),
              child: Padding(
                padding: const EdgeInsets.symmetric(
                    vertical: 15.0, horizontal: 10.0),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          _buildBadge(
                              widget.pp_visitModel.id_visite,
                              PhotoTypology[index].id_photo_typologie,
                              PhotoTypology[index].libelle),
                          Icon(Icons.chevron_right, size: 20, color: SOFT_GREY),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );

  Widget _buildBadge(int visitId, int photoTypologyId, String libelle) {
    int photoCount =
        objectbox.getVisitTypologiePhotoCount(visitId, photoTypologyId);
    if (photoCount > 0) {
      return Expanded(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              libelle,
              style: const TextStyle(
                fontSize: 15.0,
                fontWeight: FontWeight.bold,
              ),
            ),
            Container(
              margin: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
              child: badges.Badge(
                badgeStyle: badges.BadgeStyle(
                  badgeColor: Colors.blue,
                  padding: EdgeInsets.all(photoCount >= 10 ? 2 : 6),
                ),
                badgeContent: Text(photoCount.toString(),
                    style: TextStyle(color: Colors.white)),
                child: Icon(Icons.camera_alt_sharp),
              ),
            ),
          ],
        ),
      );
    } else {
      return Text(
        libelle,
        style: const TextStyle(
          fontSize: 15.0,
        ),
      );
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  FutureOr onGoBack(dynamic value) {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: GlobalStyle.appBarIconThemeColor,
        ),
        elevation: GlobalStyle.appBarElevation,
        title: Text(
          widget.pp_visitModel.name,
          style: GlobalStyle.appBarTitle,
        ),
        backgroundColor: GlobalStyle.appBarBackgroundColor,
        systemOverlayStyle: GlobalStyle.appBarSystemOverlayStyle,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: StreamBuilder<List<PhotoTypology>>(
              stream: objectbox.getPhotoTypologies(),
              builder: (context, snapshot) => ListView.builder(
                shrinkWrap: true,
                //padding: const EdgeInsets.symmetric(horizontal: 20.0),
                itemCount: snapshot.hasData ? snapshot.data!.length : 0,
                itemBuilder: _itemBuilder(snapshot.data ?? []),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
