import 'package:base_app/blocs/bloc_provider.dart';
import 'package:base_app/blocs/main_bloc.dart';
import 'package:base_app/common/common.dart';
import 'package:base_app/models/common/component_model.dart';
import 'package:base_app/models/common/version_model.dart';
import 'package:base_app/res/index.dart';
import 'package:base_app/ui/pages/common/help_page.dart';
import 'package:base_app/ui/widgets/common/com_item.dart';
import 'package:base_app/ui/widgets/common/dialog/upgrade_dialog_widget.dart';
import 'package:base_app/utils/utils.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flutter/material.dart';

/// 关于页面： 版本更新，帮助等
class AboutPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final MainBloc bloc = BlocProvider.of<MainBloc>(context);
    // 帮助
    ComponentModel help = ComponentModel(
        title: IntlUtil.getString(context, Ids.titleHelp), page: HelpPage());

    return Scaffold(
      appBar: AppBar(
        title: Text(IntlUtil.getString(context, Ids.titleAbout)),
        centerTitle: true,
      ),
      body: ListView(
        children: <Widget>[
          // logo,app名称和版本号展示
          Container(
              height: 160.0,
              alignment: Alignment.center,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Card(
                    color: Theme.of(context).primaryColor,
                    elevation: 0.0,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(6.0))),
                    child: Image.asset(
                      Utils.getImgPath('logo'),
                      width: 72.0,
                      fit: BoxFit.fill,
                      height: 72.0,
                    ),
                  ),
                  Gaps.vGap5,
                  Text(
                    AppConfig.appName + ' ' + AppConfig.version,
                    style: TextStyle(color: Colours.gray_99, fontSize: 14.0),
                  )
                ],
              ),
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(width: 0.33, color: Colours.divider))),
          // 帮助
          ComArrowItem(help),
          // 版本更新检测,versionStream
          StreamBuilder(
              stream: bloc.versionStream,
              builder:
                  (BuildContext context, AsyncSnapshot<VersionModel> snapshot) {
                VersionModel model = snapshot.data;
                return Container(
                  child: Material(
                    color: Colors.white,
                    child: ListTile(
                      onTap: () {
                        // 点击监测版本
                        if (model == null) {
                          bloc.getVersion();
                        } else {
                          if (Utils.getUpdateStatus(model.version) > 0) {
                            //NavigatorUtil.launchInBrowser(model.url, title: model.title);
                            showDialog(
                              context: context,
                              barrierDismissible: false,
                              builder: (BuildContext context) => UpgradeDialog(
                                versionModel: model,
                                appId: AppConfig.appId,
                              ),
                            );
                          }
                        }
                      },
                      title:
                          Text(IntlUtil.getString(context, Ids.titleVersion)),
                      //dense: true,
                      trailing: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Text(
                            model == null
                                ? ''
                                : (Utils.getUpdateStatus(model.version) == 0
                                    ? IntlUtil.getString(
                                        context, Ids.hint_is_new)
                                    : IntlUtil.getString(
                                        context, Ids.hint_new_version)),
                            style: TextStyle(
                                color: (model != null &&
                                        Utils.getUpdateStatus(model.version) !=
                                            0)
                                    ? Colors.red
                                    : Colors.grey,
                                fontSize: 14.0),
                          ),
                          Icon(
                            Icons.navigate_next,
                            color: Colors.grey,
                          ),
                        ],
                      ),
                    ),
                  ),
                  decoration: Decorations.bottom,
                );
              }),
        ],
      ),
    );
  }
}
