import 'package:http/http.dart' as http;
import 'package:jubo/model/person.dart';
import 'package:jubo/util/params.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:jubo/control/genericService.dart' as generic;

class AuthenticationService {
/*
  Future<http.Response> _requestPost(Map<String, String> json,String method) async {
    return http.post("$urlPrincipal$method",
        headers: {
          //"Authorization": "25D33\$W!\$AA!\$54322533\$S!\$SA!\$5432",
          "Content-Type": "application/json"
        },
        body: jsonEncode(json));
  }

  Future<http.Response> _requestGet(Map<String, String> json,String method) async {
    return http.post("$urlPrincipal$method",
        headers: {
          //"Authorization": "25D33\$W!\$AA!\$54322533\$S!\$SA!\$5432",
          "Content-Type": "application/json"
        },
        body: jsonEncode(json));
  }

 */

  Future<SharedPreferences> _storage() {
    return SharedPreferences.getInstance();
  }

  Future<void> login(String email, String password, bool rememberMe) async {
    try {
      String method = "/ws_jubo/auth/v1/login";

      http.Response result = await generic
          .requestPost({'email': email, 'pass': password}, method, "").timeout(Duration(seconds: 10));

      Map<String, dynamic> body = jsonDecode(result.body);

      //print(result.statusCode);
      //print(result.body);

      if (result.statusCode != 200 || body['error'] == true) {
        throw 'Falha de comunicação';
      }

      //m = jsonResponse.map((job) => new ModelPerson.fromJson(job));
      final m = new ModelPerson.fromJson(body['data'][0]);
      final prefs = await _storage();
      if (rememberMe) {
        prefs.setBool('_rememberMe', rememberMe);
      }
      prefs.setString('_person', m.toJson());

    } catch (e) {
      throw e;
    }
  }

  Future<bool> forgotPass(String email) async {
    try {
      String method = "/ws_jubo/auth/v1/auth_forgot";

      http.Response result =
      await generic.requestPost({'email': email}, method, "").timeout(Duration(seconds: 10));

      Map<String, dynamic> body = jsonDecode(result.body);

     // print(result.statusCode);
      //print(result.body);

      if (result.statusCode != 200 || body['error'] == true) {
        //throw 'Falha de comunicação';
        return false;
      } else {
        return true;
      }
    } catch (e) {
      return false;
      throw e;
    }
  }

  /*
  Future<String> getCompanyKey() async {
    final prefs = await _storage();
    return prefs.getString(_keyStore);
  }*/

  Future<ModelPerson> getPerson() async {
    ModelPerson m;
    final prefs = await _storage();
    return new ModelPerson.fromJson(jsonDecode(prefs.get('_person')));
  }

  Future<bool> isAuth() async {
    final prefs = await _storage();
    return prefs.containsKey('_rememberMe') && prefs.containsKey('_person');
  }

  Future<Image> getPhoto() async {
    ModelPerson m = await getPerson();
    return Image.network(
        "$urlPrincipal/ws_jubo/service/v1/person/photo/${m.id}");
  }

  Future<void> logout() async {
    final prefs = await _storage();
    prefs.remove('_person');
    prefs.remove('_rememberMe');
  }
}
