import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'activity_dialog_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

///
/// 活动弹窗接口
///
class ActivityDialog extends StatelessWidget {
  final ActivityDialogModel model;

  const ActivityDialog(this.model, {Key key}) : super(key: key);

  static Future show(BuildContext context) async {
    try {
      Map result = await NetUtil.post('/api/v1/mod/pub.flutter.current_activity_dialog', method: NetMethod.GET);
      if (!EmptyUtil.isEmpty(result)) {
        var modListData = result['data']['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          Map<String, dynamic> da = Map<String, dynamic>.from(jsonDecode(modListData));
          ActivityDialogModel model = ActivityDialogModel.fromJson(Map<String, dynamic>.from(da));
          if(!EmptyUtil.isEmpty(model) && model.isOpen == '1' ) {
            return await showCupertinoDialog(context: context, builder: (_) => ActivityDialog(model));
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
  }

  /// 跳转
  void _jumpTo(BuildContext context){
    Navigator.maybePop(context);
    if(!EmptyUtil.isEmpty(model)){
      RouterUtil.route(model, model.toJson(), context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[

            /// 图片
            GestureDetector(
              onTap: ()=> _jumpTo(context),
              behavior: HitTestBehavior.opaque,
              child: Container(
                // margin: const EdgeInsets.symmetric(horizontal: 40),
                width: MediaQuery.of(context).size.width * 0.7,
                child: CachedNetworkImage(
                  imageUrl: model?.img ?? '',
                ),
              ),
            ),

            /// 关闭按钮
            GestureDetector(
              child: Container(
                width: 30,
                height: 30,
                margin: EdgeInsets.only(top: 10),
                child: CachedNetworkImage(imageUrl: model?.btnImg ?? '',),
              ),
              onTap: () {
                Navigator.pop(context);
              },
            )
          ],
        ),
      ),
    );
  }
}
