/*
 * @Author: XianKaiQun
 * @Date: 2020-04-21 15:21:01
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-07-27 17:41:38
 * @Descripttion: 
 */
part of 'index.dart';

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  _Store get store => Provider.of(context, listen: false);
  final appGlobalStore = GetIt.I.get<AppGlobalStore>();
  final BackgroundService _backgroundService = BackgroundService();
  // int _counter = 0;
  // Timer? _timer;
  // bool _isRunningInBackground = false;

  @override
  void initState() {
    // GetIt.I.get<PushGlobalStore>().setup();
    _initializeBackgroundService();
    store.onInit();
    super.initState();
  }

  @override
  void dispose() {
    _backgroundService.disableBackgroundExecution();
    super.dispose();
  }

  Future<void> _initializeBackgroundService() async {
    await _backgroundService.initialize();
    setState(() {
      // _isRunningInBackground = _backgroundService.isRunningInBackground;
    });
  }

  void _startBackgroundTask() {
    _backgroundService.runInBackground(() {});
    setState(() {
      // _isRunningInBackground = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    // final store = Provider.of<_Store>(context, listen: true);
    // final staticAppList = store.appList;
    List staticAppList = [
      {
        'name': '工作台',
        'icon': AssetList.$main_work_png_image,
        'acIcon': AssetList.$main_work1_png_image,
        'widget': WorkPage(),
        'isNet': false,
      },
      {
        'name': '绘资讯',
        'icon': AssetList.$main_synergy_png_image,
        'acIcon': AssetList.$main_synergy1_png_image,
        'widget': SaasNewsIndexPage(),
        'isNet': false,
      },
      // {
      //   'name': '协同办公',
      //   'icon': AssetList.$main_synergy_png_image,
      //   'acIcon': AssetList.$main_synergy1_png_image,
      //   'widget': oaPackage.IndexPage(),
      //   'isNet': false,
      // },
      // {
      //   'name': '绘经营',
      //   'icon': AssetList.$main_business_png_image,
      //   'acIcon': AssetList.$main_business1_png_image,
      //   'widget': businessPackage.IndexPage(),
      // },
      {
        'name': '房管数据',
        'icon': AssetList.$main_msg_png_image,
        'acIcon': AssetList.$main_msg1_png_image,
        'widget': MessagePage(),
        'isNet': false,
      },
      {
        'name': '我的',
        'icon': AssetList.$main_mine_png_image,
        'acIcon': AssetList.$main_mine1_png_image,
        'widget': MinePage(),
        'isNet': false,
      },
    ];
    return WillPopScope(
      ///不关闭App,使App进入后台
      ///只有安卓才判断，因为这样违反了人机交互，ios可能会涉及到审核问题
      onWillPop: () async {
        if (Platform.isAndroid) {
          // MoveToBackground.moveTaskToBack();
          _startBackgroundTask();
          return false;
        }
        return true;
      },
      child: Scaffold(
        body: Consumer<_Store>(
          builder: (ctx, store, child) {
            return Stack(children: [
              IndexedStack(
                index: store.currentIndex,
                children:
                    staticAppList.map<Widget>((f) => f['widget']).toList(),
              ),
              Positioned(
                bottom: 0,
                left: 0,
                right: 0,
                child: Container(
                  color: Colors.transparent,
                  child: Wisdom.row(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        color: Color(0x08000000),
                        blurRadius: 8.pt,
                        offset: Offset(0, -2),
                      )
                    ],
                    borderRadius: BorderRadius.only(
                      topRight: Radius.circular(32.pt),
                      topLeft: Radius.circular(32.pt),
                    ),
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    padding: EdgeInsets.only(top: 10.pt, bottom: 20.pt),
                    // margin: EdgeInsets.only(bottom: 15.pt),
                    children: <Widget>[
                      for (var i = 0; i < staticAppList.length; i++)
                        Wisdom(
                          onTap: () => store.changeCurrentIndex(i),
                          color: Colors.transparent,
                          child: Consumer<_Store>(
                            builder: (ctx, store, child) {
                              bool active = store.currentIndex == i;
                              return Wisdom.column(
                                height: 50.pt,
                                minWidth: 375.pt / staticAppList.length,
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                children: <Widget>[
                                  staticAppList[i]['isNet']
                                      ? staticAppList[i]
                                          [active ? 'acIcon' : 'icon']
                                      : Image(
                                          image: staticAppList[i]
                                              [active ? 'acIcon' : 'icon'],
                                          height: 28.pt,
                                        ),
                                  WisText(
                                    staticAppList[i]['name'],
                                    size: 12.pt,
                                    color: active ? colorScheme.primary : null,
                                    height: 1,
                                  ),
                                ],
                              );
                            },
                          ),
                        ).asFlexible(flex: 1),
                    ],
                  ),
                ),
              ),
            ]);
          },
        ),
      ),
    );
  }
}
