package jp.kobe_u.cs27.newse.medicalcheck.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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 jp.kobe_u.cs27.newse.medicalcheck.dto.PersonDto;
import jp.kobe_u.cs27.newse.medicalcheck.dto.PersonForm;
import jp.kobe_u.cs27.newse.medicalcheck.entity.Person;
import jp.kobe_u.cs27.newse.medicalcheck.service.PersonManager;

/**
 *  Personを操作するAPIを定義するRESTコントローラ．
 *  練習なのできちんとRestfulにマッピング． C-R-U-D = POST-GET-PUT-DELTE
 */
@RestController
@RequestMapping("/api")
public class PersonRestController {
    @Autowired
    private PersonManager pm;

    // --------------------------- Create 系 ------------------------------------
    @PostMapping("/persons")
    public PersonDto addPerson(@RequestBody @Validated PersonForm form) {
        Person p = pm.addPerson(form.toEntity());
        return PersonDto.build(p);
    }

    // --------------------------- Read 系 ------------------------------------
    @GetMapping("/persons")
    public List<PersonDto> getAllPersons() {
        return pm.getAllPersons();
    }

    @GetMapping("/persons/{number}")
    public PersonDto getPerson(@PathVariable Long number) {
        return pm.getPerson(number);
    }

    // --------------------------- Update 系 ------------------------------------
    @PutMapping("/persons/{number}")
    public PersonDto updatePerson(@PathVariable Long number, @RequestBody @Validated PersonForm form) {
        Person p = pm.updatePerson(number, form.toEntity());
        return PersonDto.build(p);
    }


    // --------------------------- Delete 系 ------------------------------------
    @DeleteMapping("/persons/{number}")
    public boolean deletePerson(@PathVariable Long number) {
        pm.deletePerson(number);
        return true;
    }

    // -------------------------- カスタム検索系 ------------------------------------
    @GetMapping("/persons/name")
    public List<PersonDto> searchPersonsByName(@RequestParam String str) {
        return pm.searchPersonsByName(str);
    }

    @GetMapping("/persons/weight")
    public List<PersonDto> searchPersonsByWeight(@RequestParam double min, @RequestParam double max) {
        return pm.searchPersonsByWeight(min, max);
    }

    @GetMapping("/persons/height")
    public List<PersonDto> searchPersonsByHeight(@RequestParam double min, @RequestParam double max) {
        return pm.searchPersonsByHeight(min, max);
    }

    @GetMapping("/persons/birthmonth")
    public List<PersonDto> searchPersonsByBirthMonth(@RequestParam int month) {
        return pm.searchPersonsByMonth(month);
    }

}