import 'dart:convert';

import 'package:feelverapp/model/Login/login_model.dart';
import 'package:feelverapp/model/base/base.dart';
import 'package:http/http.dart' as http;


class Api<T>{



  final String _baseApi = "https://backend-uat.feelver.com/api";


  final _headerApi = {
    "Authorization": "Bearer {{ token }}",
  };
 



  Future<Response<T>> login(Object body) async {
    var _model;
    var _fail;
    var result;
   
//  (body as Map)['lang'] = allTranslations.currentLanguage;
    await _httpConnection(
            "${this._baseApi}/login", this._headerApi, body)
        .then((response) {
           print("ผลลัพ เท่ากับบบบ   "+"${response.body}");
      if (response.statusCode == 200 ) {
        _model = LoginModel.fromJson(json.decode(response.body));
      } else {
        _fail = FailModel.fromJson(json.decode(response.body));
      }
      result = new Response<T>(_model, _fail);
    });
    return result;
  }

 

  ///api environment
  Future<http.Response> _httpConnection(
      String url, Map<String, String> headers, Object body) async {
    if (body != null) {

      final response = headers != null
          ? http.post(Uri.encodeFull(url), headers: headers, body: body)
          : http.post(Uri.encodeFull(url), body: body);
      return response;
    } else {

      final response = headers != null
          ? http.get(Uri.encodeFull(url), headers: headers)
          : http.get(Uri.encodeFull(url));
      return response;
    }
  }



  dynamic _jsonDecode(String json) {
    var resJson = jsonDecode(json);
    return resJson;
  }
}

class Response<T> {
  T success;
  FailModel fail;
  
  Response(this.success, this.fail);
}
