package com.haomostudio.SpringMVCProjectTemplate.controller;

import com.haomostudio.SpringMVCProjectTemplate.common.Resp;
import com.haomostudio.SpringMVCProjectTemplate.domain.Country;
import com.haomostudio.SpringMVCProjectTemplate.service.ActivitiService;
import com.haomostudio.SpringMVCProjectTemplate.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by hxg on 2016/10/06.
 */
@Controller
public class CountryController {

    @Autowired
    CountryService countryService;

    @Autowired
    HttpServletResponse response;

    @RequestMapping(value = "/countries/create", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createCountry(
            @RequestParam(value = "country_id", required = false) Integer country_id,
            @RequestParam(value = "name", required = false) String name
    ) {
        Country item = new Country();
        item.setCountry_id(country_id);
        item.setName(name);
        countryService.create(item);

        //若request param为null,但字段定义有默认值,则需从数据库中返回结果
        //return countryService.get(id);
        return item;
    }

    @RequestMapping(value = "/countries/{countries_id}/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object deleteCountry(
            @RequestHeader(value = "X-Auth-Token") String token,
            @PathVariable(value = "countries_id") String id
    ) {
        Country item = countryService.get(id);
        if (null == item) {
            response.setStatus(404);
            return Resp.fail("the asset to be deleted doesn't exist");
        }

        countryService.delete(id);
        return Resp.succ("delete success");
    }

    @RequestMapping(value = "/countries/{countries_id}/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object editCountry(
            @RequestHeader(value = "X-Auth-Token") String token,
            @PathVariable(value = "countries_id") String id,
            @RequestParam(value = "country_id", required = false) Integer country_id,
            @RequestParam(value = "name", required = false) String name
    ) {
        Country item = countryService.get(id);
        if (null == item) {
            response.setStatus(404);
            return Resp.fail("the asset to be edited doesn't exist");
        }

        item.setCountry_id(country_id);
        item.setName(name);
        countryService.update(item);

        return countryService.get(id);
    }

    @RequestMapping(value = "/countries/{countries_id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getCountry(
            @RequestHeader(value = "X-Auth-Token") String token,
            @PathVariable(value = "countries_id") String id
    ) {
        Country item = countryService.get(id);
        if (null == item) {
            response.setStatus(404);
            return Resp.fail("the asset to be edited doesn't exist");
        }

        return item;
    }

    @RequestMapping(value = "/countries", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getcountries(
            @RequestHeader(value = "page_no") Integer pageNo,
            @RequestHeader(value = "page_size") Integer pageSize,
            @RequestHeader(value = "sort_item") String sortItem,
            @RequestHeader(value = "sort_order") String sortOrder,
            @RequestHeader(value = "filters") String filters
            ) {
        return countryService.getListWithPagingAndFilter(
                pageNo, pageSize, sortItem, sortOrder, filters);
    }
}