package com.infinite.focus.server.contents;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import com.infinite.focus.server.auth.AuthenticationController;
import com.infinite.focus.server.auth.Message;
import com.infinite.focus.server.auth.SecurityConstants;

@CrossOrigin(origins = "http://localhost:8383")
@RestController
@RequestMapping("api/content")

public class ContentController {
	
	@Autowired
	ContentPagingAndSortingRepository contentPagingAndSortingRepository;
	
	@Autowired
	ContentRepository contentRepository;
	
	public ContentController() {


	}
	
	@GetMapping("/get/contents")
	public ResponseEntity<Page<Content>> getContents(@RequestHeader(SecurityConstants.HEADER_STRING) String token,
			@RequestParam(value = "content_name", defaultValue = "") String contentName,
			@RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sortBy", defaultValue = "content_id") String sortBy,
			@RequestParam(value = "orderBy", defaultValue = "asc") String orderBy) {

		AuthenticationController.isAuthenticated(token);
		
		Pageable paging = PageRequest.of(pageNo, pageSize, orderBy.equals("desc") ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending());
		//Pageable paging = PageRequest.of(pageNo, pageSize, Sort.by(sortBy).descending());


		Page<Content> pagedResult = null;

		if (contentName == null || contentName.isEmpty()) {
			pagedResult = (Page<Content>) contentPagingAndSortingRepository.findAllContents(paging);
		} else {
			pagedResult = (Page<Content>) contentPagingAndSortingRepository.findByContentName(contentName, paging);
		}

		return new ResponseEntity<Page<Content>>(pagedResult, new HttpHeaders(), HttpStatus.OK);
	}
	

	@GetMapping("/get/content/{content_id}")
	public ResponseEntity<Content> getContentById(@PathVariable(value = "content_id") Long content_id) {
		
		Content content = contentRepository.findByContentId(content_id);

		if (content == null) {
			throw new ResponseStatusException(HttpStatus.NOT_FOUND, "The content not found.");
		}
		
		return new ResponseEntity<Content>(content, HttpStatus.OK);
	}
	
	@PostMapping("/create/content")
	@ResponseBody
	public ResponseEntity<Content> createContent(@RequestHeader(SecurityConstants.HEADER_STRING) String token, @RequestBody Content request) {

		AuthenticationController.isAuthenticated(token);
		
		return new ResponseEntity<Content>(contentRepository.save(request), HttpStatus.OK);

	}
	
	@PostMapping("/update/content")
	@ResponseBody
	public ResponseEntity<Content> updateContent(@RequestHeader(SecurityConstants.HEADER_STRING) String token, @RequestBody Content request) {

		AuthenticationController.isAuthenticated(token);
		
		Content content = contentRepository.getOne(request.getContent_id());
		content.setContent_name(request.getContent_name());
		content.setStatus(request.getStatus());
		content.setCreatedAt(content.getCreatedAt());
		content.setUpdateAt(new Date());
		
		return new ResponseEntity<Content>(contentRepository.save(content), HttpStatus.OK);

	}
	
	@DeleteMapping("/delete/content/{content_id}")
	public ResponseEntity<Message> deleteContent(@RequestHeader(SecurityConstants.HEADER_STRING) String token, @PathVariable(value = "content_id") Long content_id) {

		AuthenticationController.isAuthenticated(token);
		
		Content content = contentRepository.getOne(content_id);
		
		if(content == null) {
			throw new ResponseStatusException(HttpStatus.NOT_FOUND, "The content not found!");
		}
		
		contentRepository.deleteById(content_id);

		Message message = new Message();
		message.setMessage("The Content is deleted successfully.");
		
		return new ResponseEntity<Message>(message, HttpStatus.OK);

	}
}
