import 'dart:convert';

import 'package:feelverapp/model/Login/login_model.dart';
import 'package:feelverapp/model/base/base.dart';
import 'package:feelverapp/model/home/home_model.dart';
import 'package:feelverapp/model/register/register_model.dart';
import 'package:feelverapp/model/category/category_model.dart';
import 'package:http/http.dart' as http;

class Api<T> {
  final String _baseApi = "https://backend-uat.feelver.com/api";

  final _headerApi = {
    "Authorization":
        "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvYmFja2VuZC11YXQuZmVlbHZlci5jb21cL2FwaVwvbG9naW4iLCJpYXQiOjE1OTQ5NzIzMjYsImV4cCI6MTYwMjE3MjMyNiwibmJmIjoxNTk0OTcyMzI2LCJqdGkiOiJsM1BldUJVcTdDNldSbzVGIiwic3ViIjo1LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.fwFqcbzG2T_ZHtIcGF13Ek7nIxxwbnaE9ZXGomo-nzY",
  };

  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;
  }

  Future<Response<T>> register(Object body) async {
    var _model;
    var _fail;
    var result;

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


  Future<Response<T>> category(Object body) async {
    var _model;
    var _fail;
    var result;

    await _httpConnection("${this._baseApi}/category", this._headerApi, body)
        .then((response) {
      print("ผลลัพท์ เท่ากับ  " "${response.body}");
      if (response.statusCode == 200){

        print('testtt');

        _model = categoryModel.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);
}
