import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:mobdr/config/constant.dart';
import 'package:mobdr/config/global_style.dart';
import 'package:mobdr/main.dart';
import 'package:mobdr/ui/general/chat_us.dart';
import 'package:mobdr/ui/visit/visit_photo_tag.dart';

import 'package:mobdr/db/box_visit_photo.dart';
import 'package:mobdr/db/box_photo_typology.dart';
import 'package:mobdr/db/box_photo_competitor.dart';
import 'package:mobdr/model/visit_model.dart';

class VisitPhotoTypologyDetailPage extends StatefulWidget {
  final VisitModel pp_visitModel;
  final int pp_imageId;
  final String pp_image;
  final int pp_id_typologie;

  // Requiring data parameters
  VisitPhotoTypologyDetailPage({
    required this.pp_visitModel,
    required this.pp_imageId,
    required this.pp_image,
    required this.pp_id_typologie,
  });

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

class _VisitPhotoTypologyDetailPageState
    extends State<VisitPhotoTypologyDetailPage> {
  bool _isLoading = true;
  String _errorMessage = '';

  // Typology list
  late List<PhotoTypology> _photoTypologiesList = [];
  int _photoTypologyIndex = 0;

  List<String> _visibilities = [];

  late List<String> tagList = [];
  late VisitPhoto _visitPhoto;

  // competitors
  List<EtabCompetitor> _etabCompetitorsList = [];
  String _etabCompetitor = '';

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

    loadData(widget.pp_imageId).then((_) {
      setState(() {
        _isLoading = false;
      });
    });
  }

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

  @override
  Widget build(BuildContext context) {
    if (_isLoading) {
      return Center(child: CircularProgressIndicator());
    } else if (_errorMessage.isNotEmpty) {
      return Center(
        child: AlertDialog(
          title: Text("Erreur"),
          content: Text("La photo est introuvable"),
          actions: <Widget>[
            TextButton(
              child: Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        ),
      );
    }
    return Scaffold(
        appBar: AppBar(
            iconTheme: IconThemeData(
              color: GlobalStyle.appBarIconThemeColor,
            ),
            elevation: GlobalStyle.appBarElevation,
            title: Text(
              'Photo',
              style: GlobalStyle.appBarTitle,
            ),
            backgroundColor: GlobalStyle.appBarBackgroundColor,
            systemOverlayStyle: GlobalStyle.appBarSystemOverlayStyle),
        body: WillPopScope(
          onWillPop: () {
            // fred
            Map<String, dynamic> result = {
              'change_typologie': _photoTypologyIndex !=
                  _photoTypologiesList.indexWhere((typology) =>
                      typology.id_photo_typologie == widget.pp_id_typologie),
              'tags': tagList.join(","),
              'photo_principale': _visibilities.contains('principal') ? 1 : 0,
              'photo_privee': _visibilities.contains('private') ? 1 : 0,
            };

            // return to the parent page fred
            Navigator.pop(context, result);

            return Future.value(true);
          },
          child: Column(
            children: [
              Flexible(
                child: ListView(
                  children: [
                    Image.file(File(widget.pp_image), fit: BoxFit.cover),
                    _buildPhotoTypology(),
                    _buildPhotoVisibility(),
                    _buildPhotoTag(context),
                    _BuildPhotoCompetitor(),
                    SizedBox(height: 16)
                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey,
                      offset: Offset(0.0, 1.0), //(x,y)
                      blurRadius: 2.0,
                    ),
                  ],
                ),
                child: Row(
                  children: [
                    Container(
                      child: GestureDetector(
                        onTap: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => ChatUsPage()));
                        },
                        child: ClipOval(
                          child: Container(
                              color: SOFT_BLUE,
                              padding: EdgeInsets.all(9),
                              child: Icon(Icons.chat,
                                  color: Colors.white, size: 16)),
                        ),
                      ),
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          setState(() {
                            //_shoppingCartCount++;
                          });
                          Fluttertoast.showToast(
                              msg: 'Item has been added to Shopping Cart',
                              toastLength: Toast.LENGTH_LONG);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          padding: EdgeInsets.fromLTRB(12, 8, 12, 8),
                          margin: EdgeInsets.only(right: 8),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border.all(width: 1, color: SOFT_BLUE),
                              borderRadius: BorderRadius.all(Radius.circular(
                                      10) //         <--- border radius here
                                  )),
                          child: Text('Copier dans galerie',
                              style: TextStyle(
                                  color: SOFT_BLUE,
                                  fontWeight: FontWeight.bold)),
                        ),
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        ));
  }

  Widget _buildPhotoTypology() {
    return Container(
        margin: EdgeInsets.only(top: 12),
        padding: EdgeInsets.all(16),
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Typologie', style: GlobalStyle.sectionTitle),
            SizedBox(
              height: 16,
            ),
            Wrap(
              children: List.generate(_photoTypologiesList.length, (index) {
                return radioSize(_photoTypologiesList[index].libelle, index);
              }),
            ),
          ],
        ));
  }

  Widget radioSize(String txt, int index) {
    return GestureDetector(
      onTap: () async {
        setState(() {
          _photoTypologyIndex = index;
        });

        // save photo typology in the database
        objectbox.putPhotoTypologie(
            widget.pp_imageId, _photoTypologiesList[index].id_photo_typologie);
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(12, 8, 12, 8),
        margin: EdgeInsets.only(right: 8, top: 8),
        decoration: BoxDecoration(
            color: _photoTypologyIndex == index ? SOFT_BLUE : Colors.white,
            border: Border.all(
                width: 1,
                color: _photoTypologyIndex == index
                    ? SOFT_BLUE
                    : Colors.grey[300]!),
            borderRadius: BorderRadius.all(
                Radius.circular(10) //         <--- border radius here
                )),
        child: Text(txt,
            style: TextStyle(
                color: _photoTypologyIndex == index ? Colors.white : CHARCOAL)),
      ),
    );
  }

  Widget _checkboxVisibility({value, primaryText}) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        setState(() {
          if (_visibilities.contains(value)) {
            _visibilities.remove(value);
          } else {
            _visibilities.add(value);
          }
        });

        // save photo visibilities in the database
        objectbox.putPhotoVisibilities(widget.pp_imageId, _visibilities);
      },
      child: Row(
        children: [
          Container(
            decoration: BoxDecoration(
              border: Border.all(
                  width: 1,
                  color: (_visibilities.contains(value))
                      ? PRIMARY_COLOR
                      : BLACK77),
              borderRadius: BorderRadius.all(Radius.circular(4.0)),
            ),
            child: Padding(
              padding: const EdgeInsets.all(2),
              child: (_visibilities.contains(value))
                  ? Icon(
                      Icons.check,
                      size: 12.0,
                      color: PRIMARY_COLOR,
                    )
                  : Icon(
                      Icons.check_box_outline_blank,
                      size: 12.0,
                      color: Colors.white,
                    ),
            ),
          ),
          SizedBox(width: 16),
          Text(primaryText,
              style: TextStyle(
                  fontSize: 13,
                  color: BLACK77,
                  fontWeight: (_visibilities.contains(value))
                      ? FontWeight.bold
                      : FontWeight.normal)),
        ],
      ),
    );
  }

  Widget _buildPhotoVisibility() {
    return Container(
      margin: EdgeInsets.only(top: 12),
      padding: EdgeInsets.all(16),
      color: Colors.white,
      child: Column(
        children: [
          Row(
            children: [
              Text('Visibility', style: GlobalStyle.sectionTitle),
            ],
          ),
          SizedBox(height: 16),
          _checkboxVisibility(value: 'private', primaryText: 'Privée'),
          Divider(
            height: 32,
            color: Colors.grey[400],
          ),
          _checkboxVisibility(value: 'principal', primaryText: 'Principale')
        ],
      ),
    );
  }

  Widget _buildPhotoTag(BuildContext context) {
    return GestureDetector(
      onTap: () async {
        // Navigation to the PhotoTagPage
        final newTags = await Navigator.push<List<String>>(
          context,
          MaterialPageRoute(
            builder: (context) => PhotoTagPage(
                pp_langage: widget.pp_visitModel.langage,
                pp_id_distrib: widget.pp_visitModel.id_distrib,
                pp_photoId: this._visitPhoto.id,
                pp_currentTags: tagList),
          ),
        );

        // Checking changes made to tags
        if (newTags != null && !listEquals(tagList, newTags)) {
          setState(() {
            tagList = newTags;
          });
        }
      },
      child: Container(
        margin: EdgeInsets.only(top: 12),
        padding: EdgeInsets.all(16),
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text('Tags', style: GlobalStyle.sectionTitle),
              ],
            ),
            Wrap(
              spacing: 8.0,
              runSpacing: 8.0,
              alignment: WrapAlignment.start,
              children: tagList.map((tag) {
                return Chip(
                  label: Text(tag),
                  backgroundColor: Colors.grey[300],
                );
              }).toList(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _BuildPhotoCompetitor() {
    return Container(
        margin: EdgeInsets.only(top: 12),
        padding: EdgeInsets.all(16),
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Competitor',
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
            SizedBox(
              height: 16,
            ),
            GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTap: () {
                showModalBottomSheet<void>(
                  context: context,
                  builder: (BuildContext context) {
                    return _showCompetitionPopup();
                  },
                );
              },
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.fromLTRB(12, 8, 12, 8),
                margin: EdgeInsets.only(bottom: 16),
                decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(width: 1, color: Colors.grey[300]!),
                    borderRadius: BorderRadius.all(
                        Radius.circular(10) //         <--- border radius here
                        )),
                child: _etabCompetitor == ''
                    ? Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Icon(Icons.thumb_down, color: SOFT_BLUE),
                              SizedBox(width: 12),
                              Text('Choose Competitor',
                                  style: TextStyle(
                                      color: CHARCOAL,
                                      fontWeight: FontWeight.bold)),
                            ],
                          ),
                          Icon(Icons.chevron_right, size: 20, color: SOFT_GREY),
                        ],
                      )
                    : Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(_etabCompetitor,
                                  style: TextStyle(
                                      color: CHARCOAL,
                                      fontWeight: FontWeight.bold)),
                            ],
                          ),
                          Icon(Icons.chevron_right, size: 20, color: SOFT_GREY),
                        ],
                      ),
              ),
            ),
          ],
        ));
  }

  Widget _showCompetitionPopup() {
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter mystate) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Center(
            child: Container(
              margin: EdgeInsets.only(top: 12, bottom: 12),
              width: 40,
              height: 4,
              decoration: BoxDecoration(
                  color: Colors.grey[500],
                  borderRadius: BorderRadius.circular(10)),
            ),
          ),
          Container(
            margin: EdgeInsets.fromLTRB(16, 8, 16, 8),
            child: Text('Choose Competitor', style: GlobalStyle.chooseCourier),
          ),
          Flexible(
            child: ListView.builder(
              padding: EdgeInsets.all(16),
              itemCount: _etabCompetitorsList.length,
              itemBuilder: (BuildContext context, int index) {
                EtabCompetitor competitor = _etabCompetitorsList[index];
                return GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    setState(() {
                      _etabCompetitor = competitor.nom;
                    });

                    // save photo competitor in the database
                    objectbox.putPhotoCompetitor(
                        widget.pp_imageId, competitor.id_concurrence_lien);

                    Navigator.pop(context);
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: 16),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(competitor.nom, style: GlobalStyle.courierType),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      );
    });
  }

  /// Initializes data when the page loads.
  Future<void> loadData(int photoId) async {
    String tags = "";

    try {
      // photo typologies initialization
      _photoTypologiesList = objectbox.getPhotoTypologiesList();

      _photoTypologyIndex = _photoTypologiesList.indexWhere(
          (typology) => typology.id_photo_typologie == widget.pp_id_typologie);

      // get photo object
      _visitPhoto = objectbox.getPhotoById(photoId)!;

      // visibilities initialization
      if (_visitPhoto.photo_privee == 1) _visibilities.add('private');
      if (_visitPhoto.photo_principale == 1) _visibilities.add('principal');

      // photo tag initialization
      tags = _visitPhoto.tags;
      tagList = tags.isEmpty ? [] : _visitPhoto.tags.split(",");

      // competitor initialization
      _etabCompetitorsList =
          objectbox.getEtabCompetitorList(widget.pp_visitModel.id_etab);

      final etabCompetitor =
          objectbox.getEtabCompetitorByLink(_visitPhoto.id_concurrence_lien);
      _etabCompetitor = etabCompetitor != null ? etabCompetitor.nom : "";
    } catch (e) {
      // set errorMessage for debug
      _errorMessage = 'Error loading photo: $e';
    }
  }
}
