import 'package:app_hit_a_wod/src/models/array_response.model.dart';
import 'package:app_hit_a_wod/src/models/user_subscription.model.dart';
import 'package:app_hit_a_wod/src/providers/api.provider.dart';
import 'package:app_hit_a_wod/src/providers/auth.provider.dart';
import 'package:app_hit_a_wod/src/styles.dart';
import 'package:app_hit_a_wod/src/utils.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

class MySubscriptionsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    final _auth = Provider.of<AuthProvider>(context);
    final _api = Provider.of<ApiProvider>(context);

    return neuScaffoldDetail(
      context: context,
      title: Text("Historial de suscripciones", style: Theme.of(context).textTheme.subhead.copyWith(fontWeight: FontWeight.w500, fontSize: 18)),
      action: Container(),
      body: SingleChildScrollView(
        child: FutureBuilder(
          future: _api.getUserSubscriptions(_auth.currentUser.id),
          builder: (context, AsyncSnapshot<ArrayResponse<UserSubscription>> data) {
            if(data.hasData) {
              List<UserSubscription> items = data.data.data as List<UserSubscription>;
              final DateTime now = DateTime.now();
              return Container(
                padding: EdgeInsets.all(20),
                child: ListView.builder(
                  itemCount: items.length,
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int index) {
                    final s = items[index];
                    return Column(children: <Widget>[
                      neuContainer(Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
                        RichText(text: TextSpan(text: s.company.name, style: Theme.of(context).textTheme.subtitle, children: [
                          TextSpan(text: ": ", style: Theme.of(context).textTheme.body1),
                          TextSpan(text: s.pricing.boxes.length > 1 ? "Multibox" : s.pricing.boxes[0].name, style: Theme.of(context).textTheme.body1)
                        ])),
                        SizedBox(height: 10),
                        Row(children: <Widget>[
                          Expanded(child: neuChip(Text(s.active==1 ? "Activa" : (s.ends_at==null ? (s.start_date.difference(now).inDays > 0 ? "Por iniciarse" : "Inactiva") : "Cancelada"), textAlign: TextAlign.center, style: Theme.of(context).textTheme.display2), colors: s.active==1 ? [colors["six"], colors["six"]] : [colors["seven"], colors["seven"]])),
                          SizedBox(width: 20),
                          Expanded(child: neuChip(Text(s.pricing.name, textAlign: TextAlign.center, style: Theme.of(context).textTheme.display2), colors: primaryColors)),
                        ]),
                        SizedBox(height: 10),
                        Row(children: <Widget>[
                          Expanded(child: Text(s.end_date==null ? "Ilimitada" : "${DateFormat('dd/MM/yyyy').format(s.start_date)} - ${DateFormat('dd/MM/yyyy').format(s.end_date)}", style: Theme.of(context).textTheme.body1, overflow: TextOverflow.ellipsis)),
                          SizedBox(width: 20),
                          neuChip(Text(s.role.name, style: Theme.of(context).textTheme.display2), colors: secondaryShadow)
                        ])
                      ])),
                      SizedBox(height: 20)
                    ]);
                  },
                )
              );
            }
            else return Container(
              padding: EdgeInsets.all(20),
              child: Column(children: <Widget>[
                neuSkeletonCard(150, double.infinity),
                SizedBox(height: 20),
                neuSkeletonCard(150, double.infinity),
                SizedBox(height: 20),
                neuSkeletonCard(150, double.infinity),
                SizedBox(height: 20),
                neuSkeletonCard(150, double.infinity),
                SizedBox(height: 20),
                neuSkeletonCard(150, double.infinity),
                SizedBox(height: 20),
                neuSkeletonCard(150, double.infinity)
              ]),
            );
          }
        )
      )
    );
  }
}