package com.jakubu9333.grammarManager.api;

import com.fasterxml.jackson.annotation.JsonView;

import com.jakubu9333.grammarManager.model.Grammar;
import com.jakubu9333.grammarManager.model.View;
import com.jakubu9333.grammarManager.service.GrammarService;
import com.jakubu9333.grammarManager.service.RuleService;

import org.springframework.beans.factory.annotation.Autowired;

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 java.util.List;

/**
 * @author Jakub Uhlarik
 */
@RestController
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(path = "api/grammars")
public class GrammarController {
    public final RuleService ruleService;

    private final GrammarService grammarService;



    @Autowired
    public GrammarController(RuleService ruleService, GrammarService grammarService) {
        this.ruleService = ruleService;
        this.grammarService = grammarService;
    }

    @JsonView(View.RuleWithoutGrammar.class)
    @GetMapping("")
    public ResponseEntity<List<Grammar>> getAllGrammars() {
        return new ResponseEntity<>(grammarService.getGrammars(), HttpStatus.OK);
    }

    @JsonView(View.RuleWithoutGrammar.class)
    @GetMapping("{id}")
    public ResponseEntity<Grammar> getGrammar(@PathVariable Long id) {
        return new ResponseEntity<>(grammarService.getGrammarById(id), HttpStatus.OK);
    }

    @JsonView(View.RuleWithoutGrammar.class)
    @PostMapping("")
    public ResponseEntity<Grammar> addGrammar(@RequestBody Grammar grammar) {
        return new ResponseEntity<>(grammarService.processAddGrammar(grammar), HttpStatus.CREATED);
    }

    @DeleteMapping("/all")
    public ResponseEntity<?> deleteAllGrammar() {
        grammarService.deleteAll();
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteGrammar(@PathVariable Long id) {
        grammarService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @JsonView(View.RuleWithoutGrammar.class)
    @PostMapping("{id}")
    public ResponseEntity<Grammar> duplicateGrammar(@PathVariable Long id) {
        return new ResponseEntity<>(grammarService.duplicateGrammar(id), HttpStatus.CREATED);
    }

    @JsonView(View.RuleWithoutGrammar.class)
    @GetMapping("/root")
    public ResponseEntity<List<Grammar>> findGrammarByRootRule(@RequestParam String root) {
        return new ResponseEntity<>(grammarService.getGrammarByRootRule(root), HttpStatus.OK);
    }

    @JsonView(View.RuleWithoutGrammar.class)
    @GetMapping("/uri")
    public ResponseEntity<List<Grammar>> findGrammarByUriStart(@RequestParam String uri) {
        return new ResponseEntity<>(grammarService.getGrammarsByUriStart(uri), HttpStatus.OK);
    }

    @JsonView(View.RuleWithoutGrammar.class)
    @PutMapping("{id}/uri")
    public ResponseEntity<Grammar> changeGrammarUri(@PathVariable Long id, @RequestBody Grammar grammar) {
        return new ResponseEntity<>(grammarService.changeUri(id, grammar), HttpStatus.OK);
    }


}
