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<Community> communities;
    List<Role> roles;
    Token token;
    List<CommunityDTO> usercommunities; // = new ArrayList<CommunityDTO>();
    List<RoleDTO> userroles; // = new ArrayList<>();

    // ADDED:
    List<String> permissions; // = new ArrayList<>();
    
    List<String> authorities;
    
    public UserInfo(String username) {
        this.username = username;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //return userroles.stream().map(r -> new SimpleGrantedAuthority(r.role.rolename)).collect(Collectors.toList());
    	Collection<? extends GrantedAuthority> authorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    	if (authorities != null) {return authorities;} 
    			else {return new ArrayList<GrantedAuthority>();}
    }

    @JsonIgnore
    public List<PermissionEnum> getPermissionsAsEnum() {
        //return userroles.stream().map(r -> PermissionEnum.fromString(r.role.rolename)).collect(Collectors.toList());
        return permissions.stream().map(PermissionEnum::fromString).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 Community {
//        String communityName;
//        String communityid;
//        String description;
//        List<UserCommunity> userCommunities;
//    }

    public void setUsercommunities (ArrayList<CommunityDTO> uc) {
    	this.usercommunities = uc;
    }
    
//    @Data
//    public static class UserCommunity {
//        CommunityDTO community;
//    }

    @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 UserRole {
    //    RoleDTO role;
    //}

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

    /*@Data
    public static class UserRole {
        String communityid;
        Id id;
        Role role;

        @Data
        public static class Id {
            String communityId;
            String roleId;
            String userId;
        }
    }*/


/* --- EXAMPLE ---
{
  "userid": 193,
  "subject": "dcVuAiutp/TTO4Xe1gcodGerd6Q/nT1BHtMWMn9nqa55u3tq6A9mUkVt5ldesG7kigbuSJQ3Qn+e2ai/e3edv6GijyhyS9q02OiH14lvqGGjugiRJA1gftY38XgqLNAscX0VD81rn1JDIolDDNrmC7rNpa15kjkqiFvdCOuqQHIVg0jhEIfzZQeyZGj19AhlBUBmhmUP1Yno5trBGcI1588kLROWp9P51woeTg8/tAvhrAVuLaMv06uH+lIleyLHgRWTPahdZ/HWZFXrBtJBWy0JWZK3z1q62QpBxoSX4ZZ6y+vk4CwOOVkoShMN9xKAPrlpcjKJRyeENApk0gjDng==",
  "usercommunities": [
    {
      "community": {
        "communityid": 4,
        "communityName": "Farlock",
        "description": "Comunita' di test"
      },
      "wallet": null
    }
  ],
  "userroles": [
    {
      "id": {
        "communityId": 4,
        "roleId": 2,
        "userId": 193
      },
      "role": {
        "id": 2,
        "description": "registered",
        "rolename": "registered"
      },
      "communityid": 4
    },
    {
      "id": {
        "communityId": 4,
        "roleId": 8,
        "userId": 193
      },
      "role": {
        "id": 8,
        "description": "platform admin",
        "rolename": "platform-admin"
      },
      "communityid": 4
    }
  ],
  "roles": [
    {
      "id": 2,
      "description": "registered",
      "rolename": "registered"
    },
    {
      "id": 8,
      "description": "platform admin",
      "rolename": "platform-admin"
    }
  ],
  "communities": [
    {
      "communityid": 4,
      "communityName": "Farlock",
      "description": "Comunita' di test"
    }
  ],
  "name": "Utente",
  "surname": "D'Agosta",
  "organization": "ENEA",
  "token": {
    "value": "39433ae8-662d-4d51-a6a1-ea7f9d239c7f",
    "creation": "2021-07-28T17:08:39.000+02:00",
    "expiration": "2021-08-27T17:08:39.000+02:00"
  }
}
*/
}