
import 'package:badges/badges.dart';
import 'package:flutter/material.dart' hide Badge;
import '../../constants.dart';
import '../../generated/l10n.dart';
import '../../routes.dart';
import '../../store/store.dart';

class MobileBottomNav extends StatelessWidget {
  final int currentIndex;

  int count = 0;

  MobileBottomNav({int currentIndex = 0}) : currentIndex = currentIndex;

  @override
  Widget build(BuildContext context) {
    final cartInfos = store.state.cartInfos;
    if (cartInfos != null && cartInfos.length > 0) {
      count = 0;
      for (var i = 0; i < cartInfos.length; i++) {
        if (cartInfos[i].productList != null && cartInfos[i].productList!.length > 0) {
          count += 1;
        }
      }
    } else {
      count = 0;
    }
    return new BottomNavigationBar(
        onTap: (index) => _go(context, index),
        currentIndex: currentIndex,
        fixedColor: Theme.of(context).primaryColor,
        type: BottomNavigationBarType.fixed,
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
              icon: new Icon(Icons.store),
              label: S.of(context).home
          ),
          new BottomNavigationBarItem(
              icon: count > 0 ? Badge(
                badgeContent: Text('${count}', style: TextStyle(fontSize: 11.0, color: Colors.white),),
                child: new Icon(Icons.shopping_basket),
              ) : Icon(Icons.shopping_basket),
              label: S.of(context).shop
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.support_agent),
              label: S.of(context).support
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.person),
              label: S.of(context).me
          )
        ]
    );
  }

  _go(BuildContext context, int index) {
    if (index == currentIndex) return;
    String path = '';
    switch(index) {
      case 0:
        path = '/';
        break;
      case 1:
        path = '/shop';
        break;
      case 2:
        path = '/my-support/${Constants.BUSINESS_ID}';
        break;
      case 3:
        path = '/me';
        break;
    }
    if (path.length > 0) {
      print('go to: $path');
      Routes.router.navigateTo(
        context,
        path,
        replace: false,
      );
    }
  }
}