package br.edu.ufcg.computacao.si1.controller;

import br.edu.ufcg.computacao.si1.model.ad.SoldAd;
import br.edu.ufcg.computacao.si1.model.form.UserForm;
import br.edu.ufcg.computacao.si1.model.user.User;
import br.edu.ufcg.computacao.si1.security.auth.Token;
import br.edu.ufcg.computacao.si1.service.UserService;
import br.edu.ufcg.computacao.si1.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

import javax.ws.rs.HeaderParam;

@RestController
@RequestMapping(value = "/api/user", produces = "application/json")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<Collection<User>> getAllUsers() {
        return new ResponseEntity<>(userService.getAll(), HttpStatus.OK);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<User> getById(@PathVariable long id) {
        return new ResponseEntity<>(userService.getById(id), HttpStatus.OK);
    }

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public ResponseEntity<User> createUser(@RequestBody UserForm userForm) {
        User user = userService.create(userForm);

        if (user == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        return ResponseEntity.ok(user);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<User> deleteUser(@PathVariable long id) {
        if (userService.delete(id)) {
            return new ResponseEntity<>(HttpStatus.OK);
        }

        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<Collection<SoldAd>> login(@RequestBody UserForm userForm) {
        User user = userService.getByEmailAndPassword(userForm.getEmail(), userForm.getPassword());

        if (user == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
        Collection<SoldAd> solds = userService.salesNotifications(user.getUser_id());
        if(solds == null){
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(solds);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public ResponseEntity<Void> logout(@RequestHeader(value="Authorization") String tokenKey) {
        Token token = tokenService.getByKey(tokenKey);

        if (token != null) {
            tokenService.delete(token);

            return ResponseEntity.noContent().build();
        }

        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    }

    @RequestMapping(value = "/me", method = RequestMethod.GET)
    public ResponseEntity<User> getLoggedUser(@RequestHeader(value="Authorization") String tokenKey) {
        Token token = tokenService.getByKey(tokenKey);

        if (token == null || token == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return ResponseEntity.ok(token.getUser());
    }
}

@@ -2016,34 +2016,21 @@
 eEntity%3C
-Collection%3CSoldAd%3E
+Token
 %3E login(
@@ -2055,32 +2055,33 @@
 orm userForm) %7B%0A
+%0A
         User use
@@ -2269,24 +2269,25 @@
       %7D%0A
+%0A
         
 Collecti
@@ -2282,90 +2282,77 @@
     
-Collection%3CSoldAd%3E solds = userService.salesNotifications(user.getUser_id(
+return ResponseEntity.ok(tokenService.createSessionToken(user
 ));%0A    
     
@@ -2351,123 +2351,231 @@
     
+%7D%0A%0A
     
-if(solds == null)%7B%0A            return ResponseEntity.ok(null);%0A        %7D%0A        return ResponseEntity.ok(solds
+@RequestMapping(value = %22/sold/%7Bid%7D%22, method = RequestMethod.GET)%0A    public ResponseEntity%3CCollection%3CSoldAd%3E%3E soldAds(@PathVariable long id)%7B%0A        return ResponseEntity.ok(this.userService.salesNotifications(id)
 );%0A 
@@ -2994,31 +2994,33 @@
 UTHORIZED);%0A
-
     %7D%0A%0A
+%0A%0A
     @Request

Add new endpoint for sold items