import 'dart:convert';

import 'package:amap_location/amap_location.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:crclib/crclib.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../models/UserInfo.dart';

class UserData {
  //私有构造函数
  UserData._internal();

  //保存单例
  static UserData _singleton = new UserData._internal();

  //工厂构造函数
  factory UserData() => _singleton;

  int language = 0;
  int deviceLanguage = 0;
  double latitude = 10.7517368433358;
  double longitude = 106.600798525456;
  UserInfo basicInfo = new UserInfo();
  int totalMoney = 0; //用户总金额
  String bindCardId = '';
  bool isCanProgram = true; //用户能否发约会节目
  int picNum = 0; //用户相册的数量
  int incomeMoney = 0; //用户收入金额
  int frozenMoney = 0; //用户冻结金额
  bool isFirstTip = false; //第一次进来提示完善资料
  int mobile = 0; //是否绑定手机号
  int freeNum = 0; //免费次数
  int usedNum = 0; //使用的次数
  bool haveReview = false; //有照片在审核
  String memberEndTime = ''; //会员到期时间
  bool hasLocationPermission = false; //是否有定位权限
  bool isBannerStatus = false; //是否支付状态
  int agentId = 0; //代理id
  int redPacketSW = 0; //红包开关
  int giftSwitch = 0; //打赏礼物开关
  int bannerZF = 0; //苹果内购开关
  int sign = 0; //签到开关
  int groupQRCode = 0; //群二维码开关
  int addFdDistanceSwitch = 0; //二维码加好友距离判断开关
  int inviteCodePrice = 0; //绑定邀请码奖励
  int accountPrice = 0; //解锁账号金额
  int redPhotoPrice = 0; //红包照片金额
  int addProgramPrice = 120; //发布节目金额
  double keyboardHeight = 0; //缓存键盘高度
  bool privatyMsgPushSwitch = true; //私人消息推送开关
  bool needRequestPushPermission = false; //是否需要推送权限
  bool newDateSwitch = false; //有新约会提醒开关
  bool applyCheckSwitch = false; //查看资料提醒开关
  bool acceptCheckSwitch = false; //通过查看资料提醒开关
  bool checkPhotoSwitch = false; //查看红包照片提醒开关
  bool codeSucessSwitch = false; //邀请码申请成功提醒开关
  bool stationOpenSwitch = true; //开启电台开关
  bool myProgramOpenSwitch = true; //我的节目开关
  bool nearbyOpenSwitch = true; //附近的人开关
  bool isTranslateUser = false ;
  List contactList = [];
  bool isInChina = false;
  int homemainIndex = 0;
  String currentCity = '';

  String deviceLanguageCode;

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = new Map<String, dynamic>();
    json['language'] = language;
    json['deviceLanguage'] = deviceLanguage;
    json['latitude'] = latitude;
    json['longitude'] = longitude;
    json['basicInfo'] = jsonEncode(basicInfo).toString();
    json['totalMoney'] = totalMoney;
    json['bindCardId'] = bindCardId;
    json['isCanProgram'] = isCanProgram;
    json['picNum'] = picNum;
    json['incomeMoney'] = incomeMoney;
    json['frozenMoney'] = frozenMoney;
    json['isFirstTip'] = isFirstTip;
    json['mobile'] = mobile;
    json['freeNum'] = freeNum;
    json['usedNum'] = usedNum;
    json['haveReview'] = haveReview;
    json['memberEndTime'] = memberEndTime;
    json['isBannerStatus'] = isBannerStatus;
    json['agentId'] = agentId;
    json['redPacketSW'] = redPacketSW;
    json['giftSwitch'] = giftSwitch;
    json['bannerZF'] = bannerZF;
    json['sign'] = sign;
    json['groupQRCode'] = groupQRCode;
    json['addFdDistanceSwitch'] = addFdDistanceSwitch;
    json['inviteCodePrice'] = inviteCodePrice;
    json['accountPrice'] = accountPrice;
    json['redPhotoPrice'] = redPhotoPrice;
    json['addProgramPrice'] = addProgramPrice;
    json['keyboardHeight'] = keyboardHeight;
    json['privatyMsgPushSwitch'] = privatyMsgPushSwitch;
    json['needRequestPushPermission'] = needRequestPushPermission;
    json['newDateSwitch'] = newDateSwitch;
    json['applyCheckSwitch'] = applyCheckSwitch;
    json['acceptCheckSwitch'] = acceptCheckSwitch;
    json['checkPhotoSwitch'] = checkPhotoSwitch;
    json['codeSucessSwitch'] = codeSucessSwitch;
    json['stationOpenSwitch'] = stationOpenSwitch;
    json['myProgramOpenSwitch'] = myProgramOpenSwitch;
    json['nearbyOpenSwitch'] = nearbyOpenSwitch;
    json['isInChina'] = isInChina;
    return json;
  }

  fromLocalJson(Map<String, dynamic> data) {
    language = data['language'];
    deviceLanguage = data['deviceLanguage'];
    latitude = data['latitude'];
    longitude = data['longitude'];
    basicInfo = UserInfo.fromLocalJson(json.decode(data['basicInfo']));
    totalMoney = data['totalMoney'];
    bindCardId = data['bindCardId'];
    isCanProgram = data['isCanProgram'];
    picNum = data['picNum'];
    incomeMoney = data['incomeMoney'];
    frozenMoney = data['frozenMoney'];
    isFirstTip = data['isFirstTip'];
    mobile = data['mobile'];
    freeNum = data['freeNum'];
    usedNum = data['usedNum'];
    haveReview = data['haveReview'];
    memberEndTime = data['memberEndTime'];
    isBannerStatus = data['isBannerStatus'];
    agentId = data['agentId'];
    redPacketSW = data['redPacketSW'];
    giftSwitch = data['giftSwitch'];
    bannerZF = data['bannerZF'];
    sign = data['sign'];
    groupQRCode = data['groupQRCode'];
    addFdDistanceSwitch = data['addFdDistanceSwitch'];
    inviteCodePrice = data['inviteCodePrice'];
    accountPrice = data['accountPrice'];
    redPhotoPrice = data['redPhotoPrice'];
    addProgramPrice = data['addProgramPrice'];
    keyboardHeight = data['keyboardHeight'];
    privatyMsgPushSwitch = data['privatyMsgPushSwitch'];
    needRequestPushPermission = data['needRequestPushPermission'];
    newDateSwitch = data['newDateSwitch'];
    applyCheckSwitch = data['applyCheckSwitch'];
    acceptCheckSwitch = data['acceptCheckSwitch'];
    checkPhotoSwitch = data['checkPhotoSwitch'];
    codeSucessSwitch = data['codeSucessSwitch'];
    stationOpenSwitch = data['stationOpenSwitch'];
    myProgramOpenSwitch = data['myProgramOpenSwitch'];
    nearbyOpenSwitch = data['nearbyOpenSwitch'];
    isInChina = data['isInChina'];
  }

  bool isMan() {
    return UserData().basicInfo.sex == 1;
  }

  setKeyboardHeight(double newHeight) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var oldHeight = prefs.getDouble(Constants.KeyboardHeight);
    if (oldHeight != newHeight) {
      prefs.setDouble(Constants.KeyboardHeight, newHeight);
    }
  }

  getValidHCoin() {
    return incomeMoney - frozenMoney;
  }

  bool get isSuperVip {
    return basicInfo.isMember == 2;
  }

  bool get isVip {
    return basicInfo.isMember > 0;
  }

  int getSessionId(int friendId) {
    var myId = basicInfo.userId;
    var idStr = myId > friendId
        ? friendId.toString() + myId.toString()
        : myId.toString() + friendId.toString();

    return Crc32Zlib().convert(utf8.encode(idStr));
  }

  reset() {
    basicInfo = new UserInfo();
    bindCardId = '';
    isCanProgram = true; //用户能否发约会节目
    picNum = 0; //用户相册的数量
    incomeMoney = 0; //用户收入金额
    frozenMoney = 0;

    isFirstTip = false; //第一次进来提示完善资料
    mobile = 0; //是否绑定手机号
    freeNum = 0; //免费次数
    usedNum = 0; //使用的次数
    haveReview = false; //有照片在审核
    memberEndTime = '';
    agentId = 0;
    isBannerStatus = false;
    homemainIndex = 0;
  }

  getCurrentPosition({callback}) async {
    //使用高德定位
    var loc = await AMapLocationClient.getLocation(false);

    if (loc != null && loc.latitude != null && loc.longitude != null) {
      UserData().latitude = loc.latitude;
      UserData().longitude = loc.longitude;

      SharedPreferences prefs = await SharedPreferences.getInstance();
      await prefs.setDouble(Constants.Latitude, UserData().latitude);
      await prefs.setDouble(Constants.Longitude, UserData().longitude);
      HttpUtil().getAddress(loc.longitude, loc.latitude);
      //HttpUtil().getAddress(106.600798525456, 10.7517368433358);
      if (callback != null) callback(loc);
    }
  }

  getCurrentCityKey() {
    if (UserData().basicInfo.meetPlace == null ||
        UserData().basicInfo.meetPlace == "") return 'HoChiMinhCIty';
    var list = UserData().basicInfo.meetPlace.split('-');
    if (list.length == 2) {
      return list[1];
    } else {
      return 'HoChiMinhCIty';
    }
  }
}
