import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterappfuyou/code/YSInformation.dart';
import 'package:flutterappfuyou/code/live/YSLive.dart';
import 'package:flutterappfuyou/code/version3/YSCoupon.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'code/YSHome.dart';
import 'code/YSMine.dart';
import 'code/YSNote.dart';
import 'code/base/YSTools.dart';
import 'code/version3/view/YSCouponCardView.dart';

class YSPage extends StatefulWidget {
  @override
  _YSPageState createState() => _YSPageState();
}

class _YSPageState extends State<YSPage> {
  int _selectedIndex = 0;
  int _refresh = 1;
  final JPush jpush = new JPush();
  @override
  void initState() {
    super.initState();
    setupLicense();
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    _prefs.then((value) => {
      if(value.getInt('chapters')==null){
        value.setInt('chapters', 0),
        value.setInt('childId', 1),
      }
    });
    jpush.setup(
      appKey: "5cdf5bc06073b6ba4b3eaf86", //你自己应用的 AppKey
      channel: "theChannel",
      production: false,
      debug: true,
    );
    jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true)
    );
    jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid");
    });
    try {
      jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
            print("flutter onReceiveNotification: $message");
          },
          onOpenNotification: (Map<String, dynamic> message) async {
            print("flutter onOpenNotification: $message");
            Navigator.of(context).push(
                CupertinoPageRoute(
                    builder: (context){
                      return YSInformation();
                    }
                )
            );
          },
          onReceiveMessage: (Map<String, dynamic> message) async {
            print("flutter onReceiveMessage: $message");
          },
          onReceiveNotificationAuthorization:(Map<String, dynamic> message) async {
            print("flutter onReceiveNotificationAuthorization: $message");
          });
    } on PlatformException {

    }
  }
  void onItemTap(int index){
    setState(() {
      if(index==2){
        _refresh++;
      }
      _selectedIndex = index;
    });
  }
  @override
  Widget build(BuildContext context) {
    return NotificationListener<CustomerValueNotification>(
      onNotification: (value){
        Map map = value.value;
        Map item = map['item'];
        List array = map['array'];
        ysShowBottomAlertView(context, YSCodeAlertView(array: array,item: item,),isBarr: true);
        return true;
      },
      child: CupertinoTabScaffold(
        tabBar: CupertinoTabBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: '首页'
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.live_tv),
                label: '直播'
            ),
            BottomNavigationBarItem(
                icon: Image.asset('lib/images/${_selectedIndex==2?'fy_yhj':'暂停'}.png',height: 20,width: 20,),
                label: '两筛'
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.supervised_user_circle),
                label: '我的'
            ),
          ],
          currentIndex: _selectedIndex,
          onTap: onItemTap,
          activeColor: Color(0xFFDB5278),
          inactiveColor: Color(0xFFBCBCBC),
          iconSize: 25,
          border: Border(top: BorderSide(color: Colors.grey,width: 0.2)),
        ),
        tabBuilder: (context,index){
          return CupertinoTabView(
            builder: (context){
              if (index == 0) {
                return YSHome();
              }else if(index == 1){
                return YSLive();
              }else if(index == 2){
                return YSCoupon(key: Key('${_refresh}YS'),);
              }else{
                return YSMine();
              }
            },
          );
        },
      ),
    );
  }
}
