import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/about_us_page/about_us_page_sk.dart';
import 'package:zhiying_base_widget/pages/about_us_page/bloc/about_us_bloc.dart';
import 'package:zhiying_base_widget/pages/about_us_page/bloc/about_us_repository.dart';
import 'package:zhiying_base_widget/pages/about_us_page/model/about_us_model.dart';
import 'package:zhiying_comm/util/update/app_update_util.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'bloc/about_us_bloc.dart';
import 'bloc/about_us_event.dart';
import 'bloc/about_us_state.dart';

///
/// 关于我们
///

class AboutUsPage extends StatelessWidget {
  final Map<String, dynamic> data;

  AboutUsPage(this.data);

  @override
  Widget build(BuildContext context) {
    return BlocProvider<AboutUsBloc>(
      create: (_) => AboutUsBloc(AboutUsRepository())..add(AboutUsInitEvent(data)),
      child: _AboutUsPageContainer(),
    );
  }
}

class _AboutUsPageContainer extends StatefulWidget {
  @override
  __AboutUsPageContainerState createState() => __AboutUsPageContainerState();
}

class __AboutUsPageContainerState extends State<_AboutUsPageContainer> {
  /// 检查更新
  void _onClickCheckUpdate() async{
    Logger.log('点击了更新');
    AppUpdateUtil.updateApp(context, needToast: true, mustShowDialog: true);
  }

  /// 给个评价
  void _onClickEvaluation() {
    Logger.log('点击了评价');
  }

  /// list 子点击
  void _onClickListItem(Settings model) {
    if (model.type == 'check_app') {
      _onClickCheckUpdate();
    } else if (model.type == 'comment') {
      _onClickEvaluation();
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AboutUsBloc, AboutUsState>(
      builder: (context, state) {
        if (state is AboutUsLoadedState) {
          return _buildMainWidget(state?.model);
        }
        return _buildSkeletion();
      },
    );
  }

  /// 主视图
  Widget _buildMainWidget(AboutUsModel model) {
    return Scaffold(
      backgroundColor: HexColor.fromHex(model?.bgColor ?? '#F9F9F9'),
      appBar: _buildAppBarWidget(null),
      body: Padding(
        padding: const EdgeInsets.only(top: 5),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            _buildHeadWidget(model),

            /// 文字
            Expanded(child: Padding(padding: const EdgeInsets.only(bottom: 21), child: _buildCompanyInfoWidget(model))),
          ],
        ),
      ),
    );
  }

  /// 骨架图
  Widget _buildSkeletion() {
    return Scaffold(
      appBar: _buildAppBarWidget(null),
      body: AboutUsPageSkeleton(),
    );
  }

  /// Icon等信息
  Widget _buildHeadWidget(AboutUsModel model) {
    return Container(
      padding: const EdgeInsets.only(top: 25, left: 12.5, right: 12.5),
      width: double.infinity,
      decoration: BoxDecoration(
        color: HexColor.fromHex(model?.settingsBgColor ?? '#FFFFFF'),
      ),
      child: Column(
        children: <Widget>[
          /// icon
          CachedNetworkImage(
            imageUrl: model?.appLogo ?? '',
            width: 84,
          ),
          const SizedBox(height: 7.5),

          /// 版本号
          RichText(
            text: TextSpan(children: [
              TextSpan(
                  text: model?.appVersionText ?? '版本号',
                  style: TextStyle(
                    fontSize: 11,
                    color: HexColor.fromHex(model?.appVersionColor ?? '#999999'),
                  )),
              TextSpan(
                  text: ' ${model?.appVerison ?? '1.0.0'}',
                  style: TextStyle(
                    fontSize: 11,
                    color: HexColor.fromHex(model?.appVersionColor ?? '#999999'),
                  )),
            ]),
          ),
          const SizedBox(height: 20),

          /// 更新，评价
          _buildListWidget(model),
        ],
      ),
    );
  }


  /// 列表
  Widget _buildListWidget(AboutUsModel model) {
    List<Widget> widgets = [];
    int length = model?.settings?.length ?? 0;
    if (length == 0) {
      widgets.add(Container());
    } else {
      model.settings.forEach((element) {
        widgets.add(GestureDetector(
          onTap: () => _onClickListItem(element),
          behavior: HitTestBehavior.opaque,
          child: Container(
            margin: const EdgeInsets.symmetric(vertical: 15),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  element?.name ?? '',
                  style: TextStyle(color: HexColor.fromHex(element?.nameColor ?? '#333333'), fontSize: 13, fontWeight: FontWeight.bold),
                ),
                Icon(
                  Icons.arrow_forward_ios,
                  color: HexColor.fromHex(element?.descColor ?? '#999999'),
                  size: 14,
                )
              ],
            ),
          ),
        ));
      });
      return Column(
        children: widgets,
      );
    }
  }

  /// 公司信息等
  Widget _buildCompanyInfoWidget(AboutUsModel model) {
    return Align(
      alignment: Alignment.bottomCenter,
      child: Text(
        model?.bottomName ?? '',
        style: TextStyle(fontSize: 11, color: HexColor.fromHex(model?.bottomNameColor ?? '#999999')),
      ),
    );
  }

  /// appbar
  Widget _buildAppBarWidget(AboutUsModel model) {
    return AppBar(
      backgroundColor: HexColor.fromHex(model?.appBarBgColor ?? '#FFFFFF'),
      brightness: Brightness.light,
      leading: IconButton(
        icon: Icon(
          Icons.arrow_back_ios,
          size: 22,
          color: HexColor.fromHex('#333333'),
        ),
        onPressed: () => Navigator.maybePop(context),
      ),
      title: Text(
        model?.appBarName ?? '关于我们',
        style: TextStyle(color: HexColor.fromHex(model?.appBarNameColor ?? '#333333'), fontSize: 15, fontWeight: FontWeight.bold),
      ),
      centerTitle: true,
      elevation: 0,
    );
  }
}
