import 'package:shared_preferences/shared_preferences.dart';

import '../../models/equipe_modelo.dart';

class PrefService{

  Future createCacheidUsuario(String idUsuario)async{

    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.setString('idUsuario', idUsuario);
  }

  Future readCacheIDUser() async{
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    String idUsuario = _preferences.getString('idUsuario')!;
    return idUsuario;
  }

  Future createCacheUnidade(String idUnidade, String nomeUnidade)async{

    print('idUnidade $idUnidade');
    print('nomeUnidade $nomeUnidade');

    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.setString('idUnidade', idUnidade);
    _preferences.setString('nomeUnidade', nomeUnidade);
    return [_preferences.getString('idUnidade'),_preferences.getString('nomeUnidade')];
  }
  Future readCacheUnidade() async{
    SharedPreferences _preferences = await SharedPreferences.getInstance();
      return [_preferences.getInt('idUnidade'),_preferences.getString('nomeUnidade')];
  }
  Future createCacheSync(EquipeModelo equipe,bool foraArea)async{

    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.setInt('idAtbArea',equipe.ID_ATB_AREA);
    _preferences.setInt('area',equipe.AREA);
    _preferences.setInt('idAtbMicroarea',equipe.ID_ATB_MICROAREA==null?0:equipe.ID_ATB_MICROAREA);
    _preferences.setInt('microarea',equipe.MICROAREA==null?0:equipe.MICROAREA);
    _preferences.setInt('idEquipe',equipe.ID_EQUIPE);
    _preferences.setInt('idUnidade',equipe.ID_UNIDADE);
    _preferences.setString('ine',equipe.INE);
    _preferences.setInt('idProfissional',equipe.ID_PROFISSIONAL);
    _preferences.setInt('ativo',equipe.ATIVO);
    _preferences.setBool('foraArea', foraArea);
  }
  Future createCacheModel(String user,String password,String city,String mode)async{

    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.setString('username', user);
    _preferences.setString('password', password);
    _preferences.setString('city', city);
    _preferences.setString('mode', mode);
  }
  Future readCacheIdUsername(String username) async{
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    var username = _preferences.getString('username');
    return username;
  }
  Future readCacheModel(String user,String password,String city,String mode) async{
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    String? username = _preferences.getString('username');
    String? password = _preferences.getString('password');
    String? city = _preferences.getString('city');
    String? mode = _preferences.getString('mode');
    List list =[
      user,
      password,
      city,
      mode
    ];
    return list;
  }
  Future readCacheSync() async{
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    int? AREA_ID = _preferences.getInt('idAtbArea');
    int? AREA_DESCRICAO = _preferences.getInt('area');
    int? MICROAREA_ID = _preferences.getInt('idAtbMicroarea');
    int? MICROAREA_DESCRICAO = _preferences.getInt('microarea');
    int? idEquipe = _preferences.getInt('idEquipe');
    int? idUnidade = _preferences.getInt('idUnidade');
    String? ine = _preferences.getString('ine');
    int? idProfissional = _preferences.getInt('idProfissional');
    int? ativo = _preferences.getInt('ativo');
    bool? foraArea = _preferences.getBool('foraArea');
    List list =[
      AREA_ID,
      AREA_DESCRICAO,
      MICROAREA_ID,
      MICROAREA_DESCRICAO,
      idEquipe,
      idUnidade,
      ine,
      idProfissional,
      ativo,
      foraArea
    ];
    return list;
  }

  Future removeCache() async{
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.remove('username');
  }
  Future removeUnidade() async{
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.remove('idUnidade');
    _preferences.remove('nomeUnidade');
  }
}