package ovh.herisson.Clyde.Services;

import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ProtectionService {

    /** return user's data except password
     * @param user the user to return
     * @return all the user data without the password
     */
    public static HashMap<String,Object> userWithoutPassword(User user){

        if (user ==null)
            return null;

        HashMap<String,Object> toReturn = new HashMap<>();

        toReturn.put("regNo",user.getRegNo());
        toReturn.put("lastName",user.getLastName());
        toReturn.put("firstName",user.getFirstName());
        toReturn.put("email", user.getEmail());
        toReturn.put("address",user.getAddress());
        toReturn.put("birthDate",user.getBirthDate());
        toReturn.put("country",user.getCountry());
        toReturn.put("profilePictureUrl",user.getProfilePictureUrl());
        toReturn.put("role",user.getRole());
        return toReturn;
    }

    public static Iterable<HashMap<String ,Object>>usersWithoutPasswords(Iterable<User> users){
        ArrayList<HashMap<String,Object>> toReturn = new ArrayList<>();

        for (User u : users){
            toReturn.add(userWithoutPassword(u));
        }

        return toReturn;
    }



    public static HashMap<String,Object> courseWithoutPassword(Course course){
        if (course == null)
            return null;

        HashMap<String ,Object> toReturn = new HashMap<>();

        toReturn.put("courseId",course.getCourseID());
        toReturn.put("credits",course.getCredits());
        toReturn.put("title", course.getTitle());
        toReturn.put("owner", userWithoutPassword(course.getOwner()));
        return toReturn;
    }

    public static Iterable<HashMap<String ,Object>> coursesWithoutPasswords(Iterable<Course> courses){
        ArrayList<HashMap<String,Object>> toReturn = new ArrayList<>();

        for (Course course: courses){
            toReturn.add(ProtectionService.courseWithoutPassword(course));
        }

        return toReturn;

    }


    public static Map<String, Object> requestWithoutPassword(InscriptionRequest inscriptionRequest) {

        if (inscriptionRequest == null)
            return null;

        Map<String, Object> toReturn = new HashMap<>();

        toReturn.put("id", inscriptionRequest.getId());
        toReturn.put("lastName", inscriptionRequest.getLastName());
        toReturn.put("firstName", inscriptionRequest.getFirstName());
        toReturn.put("address", inscriptionRequest.getAddress());
        toReturn.put("email",inscriptionRequest.getEmail());
        toReturn.put("birthDate", inscriptionRequest.getBirthDate());
        toReturn.put("country", inscriptionRequest.getCountry());
        toReturn.put("curriculum", inscriptionRequest.getCurriculumId());
        toReturn.put("state", inscriptionRequest.getState());
        toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture());

        return toReturn;
    }

    public static Iterable<Map<String ,Object>> requestsWithoutPasswords(Iterable<InscriptionRequest> inscriptionRequests){

        ArrayList<Map<String,Object>> toReturn = new ArrayList<>();

        for (InscriptionRequest i:inscriptionRequests){
            toReturn.add(requestWithoutPassword(i));
        }
        return toReturn;
    }

}

