import 'package:flutter/material.dart';
import 'package:flutter_habit/common/BaseArchitectural.dart';
import 'package:flutter_habit/common/I18N.dart';
import 'package:flutter_habit/provider/UserProvider.dart';
import 'package:flutter_habit/view/context/ExerciseInfoContext.dart';
import 'package:flutter_habit/view/context/LifeInfoContext.dart';
import 'package:flutter_habit/view/context/BasicInfoContext.dart';
import 'package:flutter_habit/view/context/StudyInfoContext.dart';
import 'package:flutter_habit/view/drawer/HomePageDrawer.dart';
import 'package:flutter_habit/view/record/BasicInfoRecordingPage.dart';
import 'package:flutter_habit/view/record/ExerciseInfoRecordingPage.dart';
import 'package:flutter_habit/view/record/LifeInfoRecordingPage.dart';
import 'package:flutter_habit/view/record/StudyInfoRecordingPage.dart';
import 'package:provider/provider.dart';

/// Description: Home Page
/// Time       : 08/18/2023 Friday
/// Author     : liuyuqi.gov@msn.cn
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<HomePageService>(
            create: (_) => HomePageService(context)),
        ChangeNotifierProvider<HomePageModel>(
            create: (_) => HomePageModel(context)),
      ],
      child: _HomePageView(),
    );
  }
}

// model
class HomePageModel extends BaseModel {
  HomePageModel(BuildContext context) : super(context);

  int currentIndex;

  PageController pageViewController;

  @override
  void init(BuildContext context) {
    super.init(context);
    currentIndex = 0;
    pageViewController = PageController(initialPage: 0);
  }
}

// service
class HomePageService extends BaseProvider {
  HomePageService(BuildContext context) : super(context);

  void changeNavigation(BuildContext context, int index) {
    HomePageModel model = Provider.of<HomePageModel>(context, listen: false);
    model.currentIndex = index;
    model.pageViewController.animateToPage(index,
        duration: Duration(milliseconds: 300), curve: Curves.ease);
    model.refresh();
  }

  void onPageChanged(BuildContext context, int index) {
    HomePageModel model = Provider.of<HomePageModel>(context, listen: false);
    model.currentIndex = index;
    model.refresh();
  }

  Future<void> toBasicInfoRecordingPage(BuildContext context) async {
    await Navigator.of(context)
        .push(MaterialPageRoute(builder: (_) => BasicInfoRecordingPage()));
  }

  Future<void> toLifeInfoRecordingPage(BuildContext context) async {
    await Navigator.of(context)
        .push(MaterialPageRoute(builder: (_) => LifeInfoRecordingPage()));
  }

  Future<void> toExerciseInfoRecordingPage(BuildContext context) async {
    await Navigator.of(context)
        .push(MaterialPageRoute(builder: (_) => ExerciseInfoRecordingPage()));
  }

  Future<void> toStudyInfoRecordingPage(BuildContext context) async {
    await Navigator.of(context)
        .push(MaterialPageRoute(builder: (_) => StudyInfoRecordingPage()));
  }
}

// view
class _HomePageView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    HomePageService service =
        Provider.of<HomePageService>(context, listen: false);
    HomePageModel model = Provider.of<HomePageModel>(context, listen: true);
    UserProvider userProvider =
        Provider.of<UserProvider>(context, listen: true);
    return Scaffold(
      appBar: AppBar(
        title: Text("Home"),
        actions: userProvider.token == null
            ? []
            : <Widget>[
                Row(
                  children: <Widget>[
                    Text(
                      "${userProvider.coins} x ",
                      style: TextStyle(fontSize: 16),
                    ),
                    Icon(Icons.monetization_on),
                    Text("  "),
                  ],
                ),
              ],
      ),
      drawer: HomePageDrawer(),
      body: PageView(
        controller: model.pageViewController,
        onPageChanged: (index) => service.onPageChanged(context, index),
        physics: NeverScrollableScrollPhysics(),
        children: <Widget>[
          BasicInfoContext(),
          LifeInfoContext(),
          ExerciseInfoContext(),
          StudyInfoContext(),
        ],
      ),
      floatingActionButton: <Widget>[
        FloatingActionButton(
          child: Icon(Icons.playlist_add),
          onPressed: () => service.toBasicInfoRecordingPage(context),
        ),
        FloatingActionButton(
          child: Icon(Icons.playlist_add),
          onPressed: () => service.toLifeInfoRecordingPage(context),
        ),
        FloatingActionButton(
          child: Icon(Icons.playlist_add),
          onPressed: () => service.toExerciseInfoRecordingPage(context),
        ),
        FloatingActionButton(
          child: Icon(Icons.playlist_add),
          onPressed: () => service.toStudyInfoRecordingPage(context),
        ),
      ][model.currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: model.currentIndex,
        onTap: (index) => service.changeNavigation(context, index),
        showUnselectedLabels: true,
        fixedColor: Theme.of(context).colorScheme.secondary,
        unselectedItemColor: Theme.of(context).unselectedWidgetColor,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(Icons.accessibility), label: "基本信息"),
          BottomNavigationBarItem(icon: Icon(Icons.wb_sunny), label: "日常生活"),
          BottomNavigationBarItem(
              icon: Icon(Icons.directions_bike), label: "体育锻炼"),
          BottomNavigationBarItem(icon: Icon(Icons.school), label: "课程学习"),
        ],
      ),
    );
  }
}
