package com.infinite.focus.server.auth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.server.ResponseStatusException;

import com.infinite.focus.server.auth.Account;
import com.infinite.focus.server.auth.AccountRepository;
import com.infinite.focus.server.auth.Admin;
import com.infinite.focus.server.auth.AdminRepository;
import com.infinite.focus.server.auth.Instructor;
import com.infinite.focus.server.auth.InstructorRepository;
import com.infinite.focus.server.auth.Message;
import com.infinite.focus.server.auth.NotFoundException;
import com.infinite.focus.server.auth.SecurityConstants;
import com.infinite.focus.server.auth.Student;
import com.infinite.focus.server.auth.StudentRepository;

import io.jsonwebtoken.Jwts;

@Service
public class AuthServiceImpl implements AuthService {

	@Autowired
	AccountRepository accountRepository;

	@Autowired
	AdminRepository adminRepository;
	
	@Autowired
	InstructorRepository instructorRepository;
	
	@Autowired
	StudentRepository studentRepository;

	@Override
	public boolean isAuthenticated(String token) {

		if (token != null && !token.contains("undefined")) {

			// Parse token
			String user = Jwts.parser().setSigningKey(SecurityConstants.SECRET)
					.parseClaimsJws(token.replace(SecurityConstants.TOKEN_PREFIX, "")).getBody().getSubject();

			// If token was successfully parsed
			if (user != null) {

				// System.out.println(user);

				// Find account for parsed user name
				Account a = accountRepository.findByUsername(user);

				// If account is null return error code to client
				// If account is null return error code to client
				if (a == null) {
					a = accountRepository.findByUsername2(user);
					if (a == null) {
						throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "The user not found!!!.");

					}
				}

				return true;
			}

			throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "The user not found!!!.");

		} else {
			throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
		}
	}

	@Override
	public boolean isAdmin(String token) {

		if (token != null && !token.contains("undefined")) {

			// Parse token
			String user = Jwts.parser().setSigningKey(SecurityConstants.SECRET)
					.parseClaimsJws(token.replace(SecurityConstants.TOKEN_PREFIX, "")).getBody().getSubject();

			// If token was successfully parsed
			if (user != null) {

				// System.out.println(user);

				// Find account for parsed user name
				Account a = accountRepository.findByUsername(user);

				// If account is null return error code to client
				// If account is null return error code to client
				if (a != null) {
					Admin ad = adminRepository.findByUserName(a.getUsername());
					if (ad != null) {
						return true;
					}
				}

				throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "The user is not admin!!!.");
			}

			throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "The user not found!!!.");

		} else {
			throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
		}
	}
	
	@Override
	public Instructor isInstructor(String token) {

		if (token != null && !token.contains("undefined")) {

			// Parse token
			String user = Jwts.parser().setSigningKey(SecurityConstants.SECRET)
					.parseClaimsJws(token.replace(SecurityConstants.TOKEN_PREFIX, "")).getBody().getSubject();

			// If token was successfully parsed
			if (user != null) {

				// System.out.println(user);

				// Find account for parsed user name
				Account a = accountRepository.findByUsername(user);

				// If account is null return error code to client
				// If account is null return error code to client
				if (a != null) {
					Instructor i = instructorRepository.findByAccountId(a.getAccount_id());
					if (i != null) {
						return i;
					}
				}

				throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "The user is not Instructor!!!.");
			}

			throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "The user not found!!!.");

		} else {
			throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
		}
	}
	
	@Override
	public Student isStudent(String token) { // Capture data sent in Request Body

		if (token != null && !token.contains("undefined")) {

			// Parse token
			String user = Jwts.parser().setSigningKey(SecurityConstants.SECRET)
					.parseClaimsJws(token.replace(SecurityConstants.TOKEN_PREFIX, "")).getBody().getSubject();

			// If token was successfully parsed
			if (user != null) {

				// System.out.println(user);

				// Find account for parsed user name
				Account a = accountRepository.findByUsername(user);

				// If account is null return error code to client
				if (a == null) {
					a = accountRepository.findByUsername2(user);
					if (a == null) {
						throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found!!!");
					}
				}

				Student student = studentRepository.findByAccountId(a.getAccount_id());

				// If account is null return error code to client
				if (student == null) {
					throw new ResponseStatusException(HttpStatus.NOT_FOUND, "The User is not a Student!!!");
				}

				return student;
			}
		}

		throw new NotFoundException("User not found!!!");
	}
}
