import 'dart:convert';
import 'dart:math';

import 'package:dartin/dartin.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sport/bean/bind_info.dart';
import 'package:sport/bean/login.dart';
import 'package:sport/provider/user_model.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/toast.dart';

class LoginInfoModel extends ChangeNotifier with InjectLoginApi {
  bool isNew=false;
  String username = "";
  void initUser() {}

  /// 登录
  Future<bool> getlogin(BuildContext context, String phone,
      {String pwd, String captcha}) async {
    LoginInfo loginInfo =
        await loginApi.getLoginInfo(phone, password: pwd, captcha: captcha);
    if (loginInfo.code == 0) {
      isNew = loginInfo.data.isNew;
      username = loginInfo.data.user.name;
      print("username -------------------------" + username);
      await saveUserInfo({}..addAll(loginInfo.data.user.toJson())..addAll(loginInfo.data.user.bindInfo?.toJson()??{}),
          token: loginInfo.data.token);
      BindInfo bindInfo = await loginApi.getBindInfo();
      if (bindInfo != null && bindInfo.data != null) {
        await saveUserInfo(bindInfo.data.toJson());
      }
      // 退出登录后 内存中没有UserModel的注入
      Provider.of<UserModel>(context, listen: false)
          .saveUserInfo(loginInfo.data.user);
      return true;
    } else {
      // Fluttertoast.showToast(
      //     msg: "${loginInfo.msg}",
      //     toastLength: Toast.LENGTH_SHORT,
      //     gravity: ToastGravity.CENTER,
      //     backgroundColor: Colors.white,
      //     textColor: Colors.black,
      //     fontSize: 13.0);
      return false;
    }
  }

  /// 保存用户信息到 sp 不太熟悉传对象 就这样将就一下
  saveUserInfo(Map<String, dynamic> params, {String token}) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    params.forEach((key, value) {
      if (value is String) {
        prefs.setString(key, value);
        print("putString $key $value");
      } else if (value is int) {
        prefs.setInt(key, value);
        print("putInt $key $value");
      }else if (value is bool) {
        prefs.setBool(key, value);
        print("putBool $key $value");
      }

    });
    // 这里的因为更新的时候不会返回token 可能会覆盖 所以...
    if (token != null) {
      prefs.setString('token', token);
    }
    print("token $token");
  }

  Future<bool> getCaptcha(String phone) async {
    var data = await loginApi.getCaptcha(phone);
    if (data['code'] == 0) {
      ToastUtil.show("验证码发送成功");
      // Fluttertoast.showToast(
      //     msg: "验证码发送成功",
      //     toastLength: Toast.LENGTH_SHORT,
      //     gravity: ToastGravity.CENTER,
      //     backgroundColor: Colors.white,
      //     textColor: Colors.black,
      //     fontSize: 13.0);
      return true;
    }
    return false;
  }

  Future<bool> verifyCaptch(String phone, String captcha) async {
    var data = await loginApi.verifyCaptcha(phone, captcha);
    if (data['code'] != 0) {
      // Fluttertoast.showToast(
      //     msg: "${data['msg']}",
      //     toastLength: Toast.LENGTH_SHORT,
      //     gravity: ToastGravity.CENTER,
      //     backgroundColor: Colors.white,
      //     textColor: Colors.black,
      //     fontSize: 13.0);
      return false;
    } else if (!data['data']) {
      ToastUtil.show("验证码输入错误");
      // Fluttertoast.showToast(
      //     msg: "验证码输入错误",
      //     toastLength: Toast.LENGTH_SHORT,
      //     gravity: ToastGravity.CENTER,
      //     backgroundColor: Colors.white,
      //     textColor: Colors.black,
      //     fontSize: 13.0);
      return false;
    }
    return true;
  }

  Future<bool> resetPasswordByCaptcha(
      String phone, String captcha, String password) async {
    var data = await loginApi.resetPasswordByCaptcha(phone, captcha, password);
    if (data['code'] != 0) {
      // Fluttertoast.showToast(
      //     msg: "${data['msg']}",
      //     toastLength: Toast.LENGTH_SHORT,
      //     gravity: ToastGravity.CENTER,
      //     backgroundColor: Colors.white,
      //     textColor: Colors.black,
      //     fontSize: 13.0);
      return false;
    }
    return true;
  }

  Future<bool> resetPasswordByOld(
      String oldPassword, String newPassword) async {
    var data = await loginApi.resetPasswordByOld(oldPassword, newPassword);
    if (data['code'] != 0) {
      // Fluttertoast.showToast(
      //     msg: "${data['msg']}",
      //     toastLength: Toast.LENGTH_SHORT,
      //     gravity: ToastGravity.CENTER,
      //     backgroundColor: Colors.white,
      //     textColor: Colors.black,
      //     fontSize: 13.0);
      return false;
    }
    return true;
  }

  Future<bool> updateUserInfo(BuildContext context, String name, int gender,
      {int age, int provinceId, int cityId, int districtId}) async {
    var loginInfo = await loginApi.updateInfo(name, gender,
        age: age,
        province_id: provinceId,
        district_id: districtId,
        city_id: cityId);
    if (loginInfo["code"] == 0) {
      await saveUserInfo(loginInfo['data']);
      Provider.of<UserModel>(context, listen: false)
          .saveUserInfo(User.fromJson(loginInfo['data']));
      return true;
    } else {
      // Fluttertoast.showToast(
      //     msg: "${loginInfo['msg']}",
      //     toastLength: Toast.LENGTH_SHORT,
      //     gravity: ToastGravity.CENTER,
      //     backgroundColor: Colors.white,
      //     textColor: Colors.black,
      //     fontSize: 13.0);
      return false;
    }
  }

//  Future<bool> updateUserInfoMap(Map<String, dynamic> params) async {
//    var loginInfo = await loginApi.updateInfoMap(params);
//
//    if (loginInfo["code"] == 0) {
//      await _saveUserInfo(loginInfo['data']);
//      return true;
//    } else {
//      Fluttertoast.showToast(
//          msg: "${loginInfo['msg']}",
//          toastLength: Toast.LENGTH_SHORT,
//          gravity: ToastGravity.CENTER,
//          backgroundColor: Colors.white,
//          textColor: Colors.black,
//          fontSize: 13.0);
//      return false;
//    }
//  }

}

//const final
