package app.usluga.web;

import org.springframework.http.HttpStatus;
import app.model.Oglas;
import app.usluga.OglasUsluga;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

@Controller
public class MainKontroler {

	 @PostMapping("/upload")
	 public ResponseEntity<?> handleFileUpload (@RequestParam("file")MultipartFile file)
	 {
		String fileName=file.getOriginalFilename();
		 try {


			 file.transferTo(new File("C:\\xampp\\htdocs\\"+fileName));

		 } catch (IOException e) {
			 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		 }
		 return ResponseEntity.ok("file uploaded successfully");

	 }
	@GetMapping("/login")
	public String prikaziLogin() {
		return "login";
	}
	
	@GetMapping("/")
	public String prikaziHome() {
		Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>)    SecurityContextHolder.getContext().getAuthentication().getAuthorities();

		System.out.println(authorities);

		 return "index";
	}






	@GetMapping("/maintance")
	public String maintance() {
		return "maintance";
	}


	@Autowired
	private OglasUsluga oglasUsluga;

	@GetMapping("/oglasi")
	public String prikazOglasa(Model model)
	{

		List<Oglas> oglasi = oglasUsluga.getAll();
		model.addAttribute("oglasi",oglasi);

		return "property";

	}

	
	
}
