import 'package:flutter/material.dart';
import 'package:zhiying_comm/pages/empty_page/bloc/empty_page_bloc.dart';
import 'package:zhiying_comm/pages/empty_page/model/empty_page_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/empty_page_bloc.dart';
import 'bloc/empty_page_event.dart';
import 'bloc/empty_page_state.dart';
import 'bloc/empty_page_repository.dart';

class EmptyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<EmptyPageBloc>(
      create: (_) => EmptyPageBloc(EmptyPageRepository())..add(EmptyPageInitEvent()),
      child: _EmptyPageContainer(),
    );
  }
}

class _EmptyPageContainer extends StatefulWidget {
  @override
  __EmptyPageContainerState createState() => __EmptyPageContainerState();
}

class __EmptyPageContainerState extends State<_EmptyPageContainer> {
  /// 返回 or 升级
  void _buttonClick(String text) {
    if(!EmptyUtil.isEmpty(text) && text == 'update'){
      // 更新
      AppUpdateUtil.updateApp(context, needToast: true, mustShowDialog: true);
    }else {
      // 返回
      Navigator.maybePop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<EmptyPageBloc, EmptyPageState>(
      builder: (context, state) {
        if (state is EmptyPageLoadedState) {
          return _buildMainWidget(state?.model);
        }
        return _buildMainWidget(null);
      },
    );
  }

  Widget _buildMainWidget(EmptyPageModel model) {
    return Scaffold(
      backgroundColor: HexColor.fromHex(model?.bgColor ?? '#F9F9F9'),
      appBar: AppBar(
        brightness: Brightness.light,
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 22,
            color: HexColor.fromHex(model?.appBarNameColor ?? '#333333'),
          ),
          onPressed: () => Navigator.maybePop(context),
        ),
        backgroundColor: HexColor.fromHex('#FFFFFF'),
        centerTitle: true,
        title: Text(
          model?.appBarName ?? '开发中',
          style: TextStyle(fontSize: 15, color: HexColor.fromHex(model?.appBarNameColor ?? '#333333'), fontWeight: FontWeight.bold),
        ),
        elevation: 0,
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 36),
        child: Column(
          children: <Widget>[
            /// 图片
            Padding(
                padding: const EdgeInsets.only(top: 125),
                child: Container(
                  height: 200,
                  width: double.infinity,
                  child: CachedNetworkImage(
                    imageUrl: model?.bgImg ?? '',
                  ),
                )),

            /// 提示文字
            Padding(
                padding: const EdgeInsets.only(top: 16, left: 30, right: 30),
                child: Text(
                  model?.contentText ?? '程序员小哥正在开发中,敬请期待',
                  maxLines: 2,
                  textAlign: TextAlign.center,
                  style: TextStyle(color: HexColor.fromHex(model?.contentTextColor ?? '#999999'), fontSize: 14),
                )),

            /// 按钮
            Padding(
              padding: const EdgeInsets.only(top: 16),
              child: GestureDetector(
                onTap: ()=>_buttonClick(model?.btnType),
                behavior: HitTestBehavior.opaque,
                child: Container(
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(30),
                      boxShadow: [
                        BoxShadow(
                          color: HexColor.fromHex('#FF2020'),
                          blurRadius: 0.5,
                        )
                      ],
                      gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
                        HexColor.fromHex(model?.btnColor1 ?? '#FF6868'),
                        HexColor.fromHex(model?.btnColor2 ?? '#FF4242'),
                      ])),
                  padding: const EdgeInsets.symmetric(horizontal: 43, vertical: 7.5),
                  child: Text(
                    model?.btnText ?? '返回',
                    style: TextStyle(color: HexColor.fromHex(model?.btnTextColor ?? '#FFFFFF'), fontSize: 13),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}