package com.itsol.quantrivanphong.access.register.controller;

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.access.register.bussiness.RegisterBussiness;
import com.itsol.quantrivanphong.access.register.dto.RegisterDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.InputMismatchException;

@RestController
@RequestMapping(path = "/user")
public class RegisterController {

    @Autowired
    private RegisterBussiness registerBussiness;



    @PostMapping(path = "/register", consumes = "application/json", produces = "application/json")
    public String register(@RequestBody RegisterDTO registerDTO)  {
        String mess ;

        try {
            if(registerDTO.getUsername().trim().equals("")) throw new InputException("Username is not null");
            if(registerDTO.getUsername().length()< 5 || registerDTO.getUsername().length() > 18) throw new InputException("Username in 5 - 18 characters");
            if(registerDTO.getLastName().equals("")) throw new InputException("Last name is not null");
            if(registerDTO.getEmailAddress().equals("")) throw new InputException("Email address is not null");
            if(registerDTO.getGraduationYear() < 0) throw new InputException("lỗi năm < 0") ;
            mess = registerBussiness.register(registerDTO);
        }catch (InputException e){
            mess = e.getMessage();
        }catch (InputMismatchException e){
           mess = e.getMessage();
        }catch (Exception e){
            mess = e.getMessage();
        }


        return mess;
    }
}
