import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:mobpush_plugin/mobpush_custom_message.dart';
import 'package:mobpush_plugin/mobpush_notify_message.dart';
import 'package:mobpush_plugin/mobpush_plugin.dart';
import 'package:zhiying_base_widget/utils/contants.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MobPushUtil {
  //接受推送监听
  static addPushReceiver() {
    void _onEventPush(Object event) {
      print('>>>>>>>>>>>>>>>>>>>>>>>>>>>我的onEvent:' + event.toString());
      Logger.log(event.toString());
      BuildContext context = navigatorKey.currentState.overlay.context;
      Map<String, dynamic> eventMap = json.decode(event);
      Map<String, dynamic> result = eventMap['result'];
      int action = eventMap['action'];

      switch (action) {
        case 0:
          MobPushCustomMessage message = new MobPushCustomMessage.fromJson(result);
          showDialog(
              context: context,
              child: AlertDialog(
                content: Text(message.content),
                actions: <Widget>[
                  FlatButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child: Text("确定"),
                  )
                ],
              ));
          break;
        //接受到推送弹出通知事件
        case 1:
          MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(result);
          print("类型1: " + result.toString());
          break;
        //点击弹窗事件
        case 2:
          try {
            MobPushNotifyMessage message = new MobPushNotifyMessage.fromJson(result);
            Map<String, dynamic> map = message.extrasMap;
            //公共跳转
            RouterUtil.route(SkipModel.fromJson(map), map, context);
            print("类型2: " + map.toString());
          } catch (e) {
            Logger.log(e);
            RouterUtil.goBackHomePage(context);
          }
          break;
      }
    }

    void _onErrorPush(Object event) {
      print('>>>>>>>>>>>>>>>>>>>>>>>>>>>onError:' + event.toString());
    }

    MobpushPlugin.addPushReceiver(_onEventPush, _onErrorPush);
  }

  //设置别名
  static setAlias(String alias) {
    MobpushPlugin.setAlias(alias).then((Map<String, dynamic> aliasMap) {
      Logger.log(aliasMap);
      String res = aliasMap['res'];
      String error = aliasMap['error'];
      String errorCode = aliasMap['errorCode'];
      print(">>>>>>>>>>>>>>>>>>>>>>>>>>> setAlias -> res: $res error: $error  errorCode: $errorCode");
    });
  }

  //获取别名
  static getAlias() {
    MobpushPlugin.getAlias().then((Map<String, dynamic> aliasMap) {
      Logger.log(aliasMap);
      String res = aliasMap['res'];
      String error = aliasMap['error'];
      print(
          ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> getAlias -> res: $res error: $error>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    });
  }

  //删除别名
  static deleteAlias() {
    MobpushPlugin.deleteAlias().then((Map<String, dynamic> aliasMap) {
      Logger.log(aliasMap);
      String res = aliasMap['res'];
      String error = aliasMap['error'];
      print(">>>>>>>>>>>>>>>>>>>>>>>>>>> deleteAlias -> res: $res error: $error");
    });
  }

  //设置可以推送
  static setCanPush() {
    if (Platform.isIOS) {
      //设置远程推送环境，向用户授权（仅 iOS)
      MobpushPlugin.setCustomNotification();
      // 开发环境 false, 线上环境 true
      MobpushPlugin.setAPNsForProduction(true);
    }
    //上传隐私协议许可
    MobpushPlugin.updatePrivacyPermissionStatus(true);
  }

  //停止推送
  static stopPush() {
    MobpushPlugin.stopPush();
  }

  //（6）重新打开推送服务
  static restartPush() {
    MobpushPlugin.restartPush();
  }

}
