import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:localstore/localstore.dart';
import 'package:quiosco_soler_2/quiosco/models/db_archivos_model.dart';

import '../../src/providers/db_provider.dart';
import '../widgets/gridview_files.dart';
import '../widgets/lateral_menu.dart';

class FilesPage extends StatefulWidget {
  const FilesPage({Key? key}) : super(key: key);

  @override
  State<FilesPage> createState() => _FilesPageState();
}

class _FilesPageState extends State<FilesPage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  final List<DbArchivos> _arch = [];
  final _db = Localstore.instance;

  dynamic _dbInfo;

  bool _isLoading = true;

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

  void _init() async{
    final items = await _db.collection('quiosco').get();
    final keys = items!.keys;
    final key = keys.last;

    final info = items[key];

    final archivos = await DBProvider.db.getFiles(info['ides'], info['idse'], info['idca']);
    archivos.forEach((element) => _arch.add(element));
    _dbInfo = info;

    setState(() => _isLoading = false);
  }

  @override
  Widget build(BuildContext context) {
    final _screenSize = MediaQuery.of(context).size;
    return Scaffold(
      key: _scaffoldKey,
      drawer: LateralMenu(page: const [0,0,1,0,0], level: 'sec', onOptionSelected: _optionHandler),
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
        centerTitle: true,
        title: Row(
          children: [
            Image.asset(
              'assets/logo.png',
              width: 32.0,
            ),
            const SizedBox(width: 8.0),
            TextButton(
              style: TextButton.styleFrom(
                  padding: EdgeInsets.zero,
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  minimumSize: const Size(0, 0)
              ),
              child: const Icon(
                Icons.menu,
                color: Color(0xFFFF0000),
              ),
              onPressed: () => _scaffoldKey.currentState!.openDrawer(),
            ),
            const Expanded(
              child: Text(
                'QUIOSCO',
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.red,
                  fontFamily: 'Helvetica Neue Black Cond',
                ),
              ),
            ),
            TextButton(
              style: TextButton.styleFrom(
                  padding: EdgeInsets.zero,
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  minimumSize: const Size(0, 0)
              ),
              child: const Icon(
                Icons.menu,
                color: Colors.transparent,
              ),
              onPressed: null,
            ),
            const SizedBox(width: 16.0),
            TextButton(
              style: TextButton.styleFrom(
                  padding: EdgeInsets.zero,
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  minimumSize: const Size(0, 0)
              ),
              child: const Icon(
                Icons.menu,
                color: Colors.transparent,
              ),
              onPressed: null,
            ),
          ],
        ),
      ),
      body: SafeArea(
        child: _isLoading ? const Center(
          child: CircularProgressIndicator(),
        ) : Column(
          children: [
            Container(
              height: 44.0,
              color: Colors.grey,
              padding: const EdgeInsets.symmetric(horizontal: 8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TextButton(
                    style: TextButton.styleFrom(
                        padding: EdgeInsets.zero,
                        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        minimumSize: const Size(0, 0)
                    ),
                    child: const Icon(
                      Icons.arrow_back,
                      color: Colors.white,
                    ),
                    onPressed: () => Navigator.pop(context),
                  ),
                  Expanded(
                    child: Text(
                      'BIENVENIDO: ${_dbInfo['user']}',
                      textAlign: TextAlign.center,
                      style: const TextStyle(
                        fontSize: 14.0,
                        fontWeight: FontWeight.bold,
                        color: Colors.white,
                      ),
                    ),
                  ),
                  TextButton(
                    style: TextButton.styleFrom(
                        padding: EdgeInsets.zero,
                        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        minimumSize: const Size(0, 0)
                    ),
                    child: const Icon(
                      Icons.home,
                      color: Colors.white,
                    ),
                    onPressed: () => Navigator.pop(context, 'home'),
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 8.0),
              width: _screenSize.width,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 4),
                    decoration: BoxDecoration(
                      color: const Color(0xFFFF0000),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    margin: const EdgeInsets.only(right: 8.0),
                    child: Text(
                      '${_arch.length}',
                      style: GoogleFonts.roboto(
                        color: Colors.white,
                        fontSize: 18.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  RichText(
                    textAlign: TextAlign.center,
                    text: TextSpan(
                      style: GoogleFonts.roboto(
                        fontWeight: FontWeight.bold,
                        fontSize: 18.0,
                      ),
                      children: [
                        TextSpan(
                          text: 'ARCHIVOS EN ',
                          style: GoogleFonts.roboto(
                            color: Colors.black,
                          ),
                        ),
                        TextSpan(
                          text: '${_dbInfo['cate'].toUpperCase()}',
                          style: GoogleFonts.roboto(
                            color: Colors.red,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 8.0, bottom: 16.0),
              alignment: Alignment.center,
              height: 4.0,
              width: 28.0,
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(8.0),
              ),
            ),
            Expanded(
              child: _arch.isNotEmpty ? GridViewArchivos(
                elements: _arch,
              ) : Center(
                child: Image.asset(
                  'assets/no_files.png',
                  width: _screenSize.width * 0.75,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void _optionHandler(String a, String? b){
    if(a == 'pop'){
      if(b == 'home'){
        Navigator.pop(context);
        Navigator.pop(context, b);
      }
    }
  }
}
