package com.realnet.dlf.Controllers;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.realnet.dlf.Entity.Design_lbrary;
import com.realnet.dlf.Services.Design_lbraryService;
import com.realnet.dlf.Services.DlfService;
import com.realnet.fnd.response.EntityResponse;

@RequestMapping(value = "/Design_lbrary")
@CrossOrigin("*")
@RestController
public class Design_lbraryController {
	@Autowired
	private Design_lbraryService designLibraryService;

	@Autowired
	private DlfService dlfService;

	@Value("${projectPath}")
	private String projectPath;

	@PostMapping("/Design_lbrary")
	public Design_lbrary Savedata(@RequestBody Design_lbrary data) {
		Design_lbrary save = designLibraryService.Savedata(data);

		System.out.println("data saved..." + save);

		return save;
	}

	@PutMapping("/Design_lbrary/{id}")
	public Design_lbrary update(@RequestBody Design_lbrary data, @PathVariable Integer id) {
		Design_lbrary update = designLibraryService.update(data, id);
		System.out.println("data update..." + update);
		return update;
	}

//	get all with pagination
	@GetMapping("/Design_lbrary/getall/page")
	public Page<Design_lbrary> getall(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size) {
		Pageable paging = PageRequest.of(page, size);
		Page<Design_lbrary> get = designLibraryService.getAllWithPagination(paging);

		return get;

	}

	@GetMapping("/Design_lbrary")
	public List<Design_lbrary> getdetails() {
		List<Design_lbrary> get = designLibraryService.getdetails();
		return get;
	}

	@GetMapping("/Design_lbrary/line/{id}")
	public List<Design_lbrary> getAllDlf(@PathVariable Integer id) {
		List<Design_lbrary> get = designLibraryService.getAllDlfByhedId(id);
		return get;
	}

// get all without authentication 

	@GetMapping("/token/Design_lbrary")
	public List<Design_lbrary> getallwioutsec() {
		List<Design_lbrary> get = designLibraryService.getdetails();
		return get;
	}

	@GetMapping("/Design_lbrary/{id}")
	public Design_lbrary getdetailsbyId(@PathVariable Integer id) {
		Design_lbrary get = designLibraryService.getdetailsbyId(id);
		return get;
	}

	@DeleteMapping("/Design_lbrary/{id}")
	public ResponseEntity<?> delete_by_id(@PathVariable Integer id) {
		designLibraryService.delete_by_id(id);
		return new ResponseEntity<>(new EntityResponse("Deleted"), HttpStatus.OK);

	}

	@DeleteMapping("/Design_lbrary/range/{startId}/{endId}")
	public ResponseEntity<?> delete_by_id(@PathVariable Long startId, @PathVariable Long endId) {
		String string = designLibraryService.deleteByRange(startId, endId);
		return new ResponseEntity<>(new EntityResponse(string), HttpStatus.OK);

	}

	@GetMapping("/preview/{id}")
	public ResponseEntity<String> getDesignPreview(@PathVariable Integer id) {
		String htmlWithCss = dlfService.generateHtmlWithCss(id);
		return ResponseEntity.ok(htmlWithCss);
	}

	@GetMapping("/Design_lbrary/build")
	public void buildFiles() throws IOException {
		designLibraryService.build();
		System.out.println(" all files build..");
	}

	// GET LINE BY header id, operation type, field type
	@GetMapping("/Design_lbrary/random")
	public ResponseEntity<?> getflflinerandom(@RequestParam String operationType, @RequestParam String fieldType) {
		Design_lbrary flf_line = designLibraryService.getflflinerandom(operationType, fieldType);

		if (flf_line == null) {

			return new ResponseEntity<>("not found", HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<Design_lbrary>(flf_line, HttpStatus.OK);
	}

	@GetMapping("/Design_lbrary/unique")
	public ResponseEntity<?> getflfline(@RequestParam String operationType, @RequestParam String fieldType) {
		Design_lbrary flf_line = designLibraryService.getLinestraight(operationType, fieldType);

		if (flf_line == null) {

			return new ResponseEntity<>("not found", HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<Design_lbrary>(flf_line, HttpStatus.OK);
	}

	@GetMapping("/Design_lbrary/line/unique")
	public ResponseEntity<?> getallByHeaderId(@RequestParam Integer headerId, @RequestParam String operationType,
			@RequestParam String fieldType) {
		Design_lbrary flf_line = designLibraryService.getallByHeaderId(headerId, operationType, fieldType);

		if (flf_line == null) {

			return new ResponseEntity<>("not found", HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<Design_lbrary>(flf_line, HttpStatus.OK);
	}

	@GetMapping("/Design_lbrary/list/template")
	public ResponseEntity<?> listOfTemplate(@RequestParam String operationType, @RequestParam String fieldType) {
		List<Design_lbrary> flf_line = designLibraryService.listOfTemplate(operationType, fieldType);

		if (flf_line == null) {

			return new ResponseEntity<>("not found", HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<>(flf_line, HttpStatus.OK);
	}

}