import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'app_state.dart';

class FavoritePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    TindBookState state = Provider.of<TindBookState>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Favorite Books'),
      ),
      body: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 0.7,
        ),
        itemCount: state.favorites.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => ImageScreen(book: state.getFav(index)),
                ),
              );
            },
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Image.asset(
                state.getFav(index)['image'],
                fit: BoxFit.cover,
              ),
            ),
          );
        },
      ),
    );
  }
}

class ImageScreen extends StatelessWidget {
  final Map<String, dynamic> book;

  const ImageScreen({Key? key, required this.book}) : super(key: key);

  String checkNull(dynamic value) {
    return value != null ? value.toString() : '';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(book['title']),
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Image.asset(book['image']),
            SizedBox(height: 16.0),
            if (book['title'] != null)
              Text(
                'Title: ${checkNull(book['title'])}',
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.black87),
              ),
            if (book['subtitle'] != null)
              Text(
                'Subtitle: ${checkNull(book['subtitle'])}',
                style: TextStyle(fontSize: 18, color: Colors.black54),
              ),
            SizedBox(height: 8.0),
            if (book['author'] != null)
              Text(
                'Author: ${checkNull(book['author'])}',
                style: TextStyle(fontSize: 16, color: Colors.black54),
              ),
            SizedBox(height: 8.0),
            if (book['published'] != null)
              Text(
                'Published: ${checkNull(book['published'])}',
                style: TextStyle(fontSize: 16, color: Colors.black54),
              ),
            SizedBox(height: 8.0),
            if (book['publisher'] != null)
              Text(
                'Publisher: ${checkNull(book['publisher'])}',
                style: TextStyle(fontSize: 16, color: Colors.black54),
              ),
            SizedBox(height: 8.0),
            if (book['pages'] != null)
              Text(
                'Pages: ${checkNull(book['pages'])}',
                style: TextStyle(fontSize: 16, color: Colors.black54),
              ),
            SizedBox(height: 8.0),
            if (book['description'] != null)
              Text(
                'Description: ${checkNull(book['description'])}',
                style: TextStyle(fontSize: 16, color: Colors.black54),
              ),
            SizedBox(height: 16.0),
            ElevatedButton(
              onPressed: () {
                //TODO: LOGIC HERE
              },
              child: Text('Remove from favorites', style: TextStyle(fontSize: 16)),
            ),
          ],
        ),
      ),
    );
  }
}
