package enea.lecservice.security.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import enea.lecservice.entities.PermissionEnum;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class UserInfo extends BaseUserInfo {
    public final static String DUMMY_TOKEN = "fe677dd7-077d-4e7d-9b3a-ec55125125a0";
    // Tuesday, November 8, 2022 10:18:53 AM -> unix timestamp with https://www.epochconverter.com/
    public final static String DUMMY_TOKEN_EXPIRATION = "1667902733000";
    public final static String DUMMY_RESPONSE = "{\"userId\":193,\"subject\":\"OM_dHx-QV6FHO8hUL6ad8JgNgSnz4WxiIA_G6vOqXmM=\",\"name\":\"Mario\",\"surname\":\"Rossi\",\"organization\":\"ENEA\",\"address\":null,\"phone\":null,\"communities\":[],\"roles\":[],\"token\":{\"creation\":1636131519000,\"expiration\":" + DUMMY_TOKEN_EXPIRATION +",\"value\":\"" + DUMMY_TOKEN + "\"},\"usercommunities\":[{\"community\":{\"communityid\":1,\"communityName\":\"SE1\",\"description\":\"Condomini pilota di SE1\",\"wallet\":\"hd-lkhjòdskjhòghDòGHaypui\"}},{\"community\":{\"communityid\":4,\"communityName\":\"Farlock\",\"description\":\"Comunita' di test\",\"wallet\":\"dhòljhà jpeoqàjàpjfaàkàhlkanhàlkjàoiteàih\"}}],\"userroles\":[{\"role\":{\"roleID\":8,\"communityID\":4,\"description\":\"platform-admin\",\"roleName\":\"platform-admin\"}}, {\"role\":{\"roleID\":2,\"communityID\":4,\"description\":\"registered\",\"roleName\":\"registered\"}}],\"permissions\":[],\"authorities\":[]}";

    String userid;
    String subject;
    String name;
    String surname;
    String organization;
    String address;
    String phone;
    String email;
    String password;
    Boolean newUser;
    List<Role> roles = new ArrayList<>();
    Token token;
    List<CommunityDTO> usercommunities = new ArrayList<CommunityDTO>();
    List<RoleDTO> userroles = new ArrayList<>();
    List<String> permissions = new ArrayList<>();
    List<String> authorities = new ArrayList<>();
    
    public UserInfo(String username) {
        this.username = username;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
    	return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override

    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Data
    public static class CommunityDTO {
        Integer communityid;
        String communityName;
        String description;
        String wallet;
        
    }

    @Data
    public static class Role {
        String id;
        String description;
        String rolename;
    }

    @Data
    public static class Token {
        Date creation;
        Date expiration;
        /*String creation;
        String expiration;*/
        String value;
    }

    @Data
    public static class RoleDTO {
        Integer roleID;
        Integer communityID;
        String description;
        String roleName;
    }
}