//import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart' show DateFormat;
import 'api/notificacoes_api.dart';
import 'api/notificacoes_classes.dart';

class NotificacoesController extends GetxController with SingleGetTickerProviderMixin{
  var notificacoesResults = <NotificacaoResults>[].obs;
  bool loadMore = true;
  NotificacoesApi _api = new NotificacoesApi();
  var notificacaoDetail = NotificacaoResponse().obs;
  int page = 1;
  var errorCode = 0.obs;
  String? errorMessage = '';
  var loading = true.obs;
  var loadingDetail = true.obs;
  var like = ''.obs;

  final f = DateFormat('dd/MM/yyyy HH:mm:ss');

  //final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();
	
  // AnimationController _controller;
  // Animation<Offset> animation;

  @override
  void onInit() {
    super.onInit();
	
      // _controller = AnimationController(
      //   duration: const Duration(milliseconds: 650),
      //   vsync: this,
      // )..forward();
      // animation = Tween<Offset>(
      //   begin: const Offset(-0.5, 0.0),
      //   end: const Offset(0.0, 0.0),
      // ).animate(CurvedAnimation(
      //   parent: _controller,
      //   curve: Curves.easeInCubic,
      // ));   
    this.notificacoesResults = new RxList<NotificacaoResults>();
    this.getNotificacoesBloc();
  }

  getNotificacoesBloc()async{
     
    var response = await this._api.getNotificacoes(page);
    this.loadMore = response.results!.length < 1 ? false : true; 
    if(response.errorCode != 0){
      this.errorCode.value = response.errorCode!;
      this.errorMessage = response.errorMessage;
    }
    else{
      //if(this.page > 1)this.listKey.currentState.insertItem(2,duration: const Duration(milliseconds: 1500));
      page++;      
      this.notificacoesResults.addAll(response.results!);
    }

    if(this.loading.value)this.loading.value = false;

  }

  getNotificacaoDetail(int? id) async {
    this.loadingDetail.value = true;
    this.notificacaoDetail.value.result = new NotificacaoResults();
    var response = await this._api.getNotificacao(id);
    this.notificacaoDetail.value = response;
    this.like.value = response.result!.like!;
    this.loadingDetail.value = false;
  }

  Future<void> refresh() async{
    this.loading.value = true;
    this.page = 1;
    this.notificacoesResults = new RxList<NotificacaoResults>();
    this.getNotificacoesBloc();
  }
  
  String get getLike => this.like.value;

  Future<void> notificacaoLikeBloc(NotificacaoLikeRequest item) async {
    var response = await this._api.notificacaoLike(item);
    //if(response != null && response.errorCode == 0)
    if(response.errorCode != null && response.errorCode == 0)
    {
      this.notificacaoDetail.value.result!.like = item.like;
      this.like.value = item.like!;
    }
  }  

  String getDateFormatada(String dt){
    String r = '';
    try {
      r = this.f.format(DateTime.parse(dt));
    } catch (e) {
    }
    return r;
  }

  Future<void> notificacaoLidaBloc(int? notificacaoID) async {
    return;
  }   
}