import 'dart:async';

import 'package:app_hit_a_wod/src/models/bi_training_session.model.dart';
import 'package:app_hit_a_wod/src/models/user.model.dart';
import 'package:app_hit_a_wod/src/providers/api.provider.dart';
import 'package:app_hit_a_wod/src/utils/fontello/my_flutter_app_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:provider/provider.dart';

import '../../styles.dart';


class neuTrainingSessionActionButton extends StatefulWidget {
  final BITrainingSession ts;
  final User user;
  final ValueChanged<BITrainingSession> onClicked;

  neuTrainingSessionActionButton(
      {this.ts,
      this.user,
      this.onClicked});

  @override
  State<StatefulWidget> createState() => new _NeuTSButton();
}

class _NeuTSButton extends State<neuTrainingSessionActionButton> {
  List<Widget> content;
  bool active;

  ApiProvider _api;
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _api = Provider.of<ApiProvider>(context);
    _generateContent(widget.ts);
    return neuButton(
      NeumorphicButton(
        boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(30)),
        padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
        style: NeumorphicStyle(
          color: Colors.transparent,
          depth: neuDepth,
        ),
        isEnabled: active,
        child: Wrap(
          crossAxisAlignment: WrapCrossAlignment.center,
          
          children: content,
          spacing: 5,
          runSpacing: 5,
          alignment: WrapAlignment.center
        ),
        onClick: () {
          onClicked: handleClickedAndUserCallback(context,widget.ts,widget.user);
        }
      ),
      colors: generateGradient(235, 235, 235),
      borderRadius: BorderRadius.circular(30),
    );
  }

  void handleClickedAndUserCallback(BuildContext context, BITrainingSession ts, User user) {

    if(ts.state=='join' || ts.state=='notify') {
      _api.joinSession(user.id, ts.id).then((newts) => {
        setState((){
          ts = newts;
          _generateContent(ts);
          _launchDateSelectionCallback(newts);
        })
      });
    } else if(ts.state=='leave') {
      _api.leaveSession(user.id, ts.id).then((newts) => {
        setState((){
          ts = newts;
          _generateContent(ts);
          _launchDateSelectionCallback(newts);
        })
      });
    } else if(ts.state=='benchmark') {
      // Ver resultados
      Navigator.pushNamed(context, 'user-benchmark-detail', arguments: {'user-benchmark': ts.benchmark, 'training-workout': ts.training_workout});
    } else if(ts.state=='done') {
      Navigator.pushNamed(context, 'user-benchmark-create', arguments: ts);
    }
  }

  void _launchDateSelectionCallback(BITrainingSession ts) {
    if (widget.onClicked != null) {
      widget.onClicked(ts);
    }
  }

  _generateContent(BITrainingSession ts) {
    active = ts.state=='notify' || ts.state=='join' || ts.state=='leave' || ts.state=='benchmark' || ts.state=='done';
    
    if (ts.state=='notify') {
      content = [
        Text('Avísame', style: TextStyle(color: colors['secondary'])),
        Icon(MyFlutterApp.paper_plane_empty, color: colors['secondary'],size: 15.0)
      ];
    } else if (ts.state=='waiting') {
      content = [
        Text('En espera', style: TextStyle(color: colors['seven'])),
        Icon(MyFlutterApp.paper_plane_empty, color: colors['seven'],size: 15.0)
      ];
    } else if (ts.state=='joined') {
      content = [
        Text('Apuntado', style: TextStyle(color: colors['seven'])),
        Icon(MyFlutterApp.kettlebell, color: colors['seven'],size: 15.0)
      ];
    } else if(ts.state=='join') {
      content = [
        Text('Apúntame', style: TextStyle(color: colors['primary'])),
        Icon(MyFlutterApp.right_arrow, color: colors['primary'],size: 15.0)
      ];
    } else if(ts.state=='leave') {
      content = [
        Text('Salir', style: TextStyle(color: colors['three'])),
        Icon(MyFlutterApp.cross, color: colors['three'],size: 15.0)
      ];
    } else if(ts.state=='benchmark') {
      content = [
        Text('Ver resultado', style: TextStyle(color: colors['four'])),
        Icon(MyFlutterApp.dumbbell, color: colors['four'],size: 15.0)
      ];
    } else if(ts.state=='done') {
      content = [
        Text('Resultados', style: TextStyle(color: colors['five'])),
        Icon(MyFlutterApp.pencil, color: colors['five'],size: 15.0)
      ];
    } else if(ts.state=='now') {
      content = [
        Text('En curso', style: TextStyle(color: colors['three'])),
        Icon(MyFlutterApp.snatch, color: colors['three'],size: 15.0)
      ];
    } else if(ts.state=='future') {
      content = [
        Text('No disponible', style: TextStyle(color: colors['seven'])),
        Icon(MyFlutterApp.depressed, color: colors['seven'],size: 15.0)
      ];
    } else if(ts.state=='past') {
      content = [
        Text('Finalizada', style: TextStyle(color: colors['six'])),
        Icon(MyFlutterApp.ok, color: colors['six'],size: 15.0)
      ];
    } else {
      content = [
        Text('${ts.state}', style: TextStyle(color: colors['three'])),
        Icon(MyFlutterApp.cross, color: colors['three'],size: 15.0)
      ];
    }
  }
}