import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:zhiying_base_widget/dialog/global_dialog/notification_setting_dialog/notification_setting_model.dart';
import 'package:zhiying_base_widget/widgets/home/home_quick_entry/cached_network_image_util.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'dart:convert' as convert;


class NotificationSettingDialog extends StatelessWidget {

  final NotificationSettingModel model;

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

  static Future show(BuildContext context) async {

    Map result =
    await NetUtil.post('/api/v1/mod/pub.flutter.push_dialog', method: NetMethod.GET);
    if (!EmptyUtil.isEmpty(result)) {
      var modListData = result['data']['mod_list'][0]['data'];
      if (!EmptyUtil.isEmpty(modListData)) {
        // return EmptyPageModel.fromJson(jsonDecode(modListData));
        //
        // String d = json['data']['data'];
        Map<String, dynamic> da =
        Map<String, dynamic>.from(convert.jsonDecode(modListData));
        NotificationSettingModel model = NotificationSettingModel.fromJson(Map<String, dynamic>.from(da));

        return await showCupertinoDialog(
            context: context, builder: (_) => NotificationSettingDialog(model));

        PermissionStatus status = await Permission.notification.status;
        if (status != PermissionStatus.granted) {
          return await showCupertinoDialog(
              context: context, builder: (_) => NotificationSettingDialog(model));

        }
      }
    }

  }

  @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>[
            Container(
              width: 280,
              // height: 382,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Column(
                children: <Widget>[
                  Stack(
                    children: <Widget>[
                      ClipRRect(
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(10),
                            topRight: Radius.circular(10)),
                        child: CachedNetworkImage(
                          fit: BoxFit.fitWidth,
                          imageUrl:
                              model?.bgStackImg ?? '',
                        ),
                      ),
                      Transform.translate(
                        offset: Offset(0, -15),
                        child: CachedNetworkImage(
                          imageUrl:
                          model?.bgOnStackImg ?? '',
                        ),
                      ),
                    ],
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      model?.title ?? '',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: HexColor.fromHex(model?.titleColor ?? '#FF4242'),
                      ),
                    ),
                  ),
                  Padding(
                    padding:
                        const EdgeInsets.only(left: 8.0, right: 8.0, bottom: 4),
                    child: Text(
                      model?.subtitle ?? '',
                      style: TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.normal,
                        color: HexColor.fromHex(model?.subtitleColor ?? '#FF4242'),
                      ),
                    ),
                  ),
                  Column(children: model?.list.map((e) => _createItem(e)).toList() ?? [],),
                  GestureDetector(
                    onTap: () async {
                      // 检查并请求权限
                      PermissionStatus status =
                          await Permission.notification.request();
                      if (status == PermissionStatus.denied) {
                        await openAppSettings();
                      } else if (status != PermissionStatus.granted) {
                        status = await Permission.notification.request();
                      }
                      Navigator.pop(context);
                      return true;
                    },
                    child: Container(
                      margin: EdgeInsets.only(top: 10, bottom: 10),
                      width: 120,
                      height: 60,
                      child: CachedNetworkImage(imageUrl: model?.btnImg ?? '',),
                    ),
                  )
                ],
              ),
            ),
            GestureDetector(
              child: Container(
                width: 30,
                height: 30,
                margin: EdgeInsets.only(top: 10),
                child: CachedNetworkImage(imageUrl: model?.deleteImg ?? '',),
              ),
              onTap: () {
                Navigator.pop(context);
              },
            )
          ],
        ),
      ),
    );
  }

  Widget _createItem(NotificationSettingItemModel itemModel) {
    return Padding(
      padding: EdgeInsets.only(left: 30, right: 30, top: 4, bottom: 4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(right: 6, top: 2),
            width: 20,
            height: 20,
            child: CachedNetworkImage(imageUrl: itemModel?.icon ?? '',)
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  itemModel?.title ?? '',
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                    color: HexColor.fromHex(model?.listItemTitleColor ?? '#333333'),
                  ),
                ),
                Text(
                  model?.subtitle ?? '',
                  style: TextStyle(
                    fontSize: 11,
                    fontWeight: FontWeight.normal,
                    color: HexColor.fromHex(model?.listItemSubtitleColor ?? '#999999'),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
