import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class SearchElement extends StatelessWidget {
  final String image;
  final String name;
  final String date;

  const SearchElement({
    Key? key,
    required this.image,
    required this.name,
    required this.date,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _screenSize = MediaQuery.of(context).size;
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0),
      child: Row(
        children: [
          FadeInImage(
            image: NetworkImage(image),
            placeholder: const AssetImage('assets/loading.gif'),
            fadeInDuration: const Duration(milliseconds: 250),
            width: 88.0,
            height: 120.0,
            fit: BoxFit.fill,
          ),
          const SizedBox(width: 8.0),
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              SizedBox(
                width: _screenSize.width-128.0,
                child: Text(
                  name,
                  style: GoogleFonts.roboto(
                    fontWeight: FontWeight.bold,
                    fontSize: 18.0,
                  ),
                ),
              ),
              SizedBox(
                width: _screenSize.width-128,
                child: Text(
                  'Publicado el ${_restartDate(date)}',
                  style: GoogleFonts.roboto(
                    fontSize: 14.0,
                    color: Colors.black54
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  String _restartDate(String date){
    String year = date.substring(2, 4);
    String dbMonth = date.substring(5, 7);
    String day = date.substring(8);
    String month = "";
    switch(dbMonth){
      case '01':
        month = 'ENE';
        break;
      case '02':
        month = 'FEB';
        break;
      case '03':
        month = 'MAR';
        break;
      case '04':
        month = 'ABR';
        break;
      case '05':
        month = 'MAY';
        break;
      case '06':
        month = 'JUN';
        break;
      case '07':
        month = 'JUL';
        break;
      case '08':
        month = 'AGO';
        break;
      case '09':
        month = 'SEP';
        break;
      case '10':
        month = 'OCT';
        break;
      case '11':
        month = 'NOV';
        break;
      case '12':
        month = 'DIC';
        break;
      default:
        month = 'MES';
        break;
    }
    return '$day-$month-$year';
  }
}
