package com.tickets.web.controller;


import com.tickets.business.services.MovieService;
import com.tickets.web.util.ErrorResult;
import com.tickets.web.util.RestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;

/**
 * Movie RESTFul web service controller.
 */
@RestController
@RequestMapping("/resource/movie")
public class MovieServiceController {
    private static final Logger LOG = LoggerFactory.getLogger(MovieServiceController.class);

    @Autowired
    private MovieService movieService;

    @RequestMapping(path = "/{id}", method = RequestMethod.GET)
    public RestResult getMovie(@PathVariable long id, HttpServletRequest request, HttpServletResponse response) {
        LOG.info(request.getMethod() + " " + request.getRequestURI());
        LinkedHashMap<String, Object> re = movieService.getMovie(id);
        if (re == null) {
            response.setStatus(404);
            return new ErrorResult("请求资源不存在");
        }
        return new RestResult(re);
    }

    @RequestMapping(path = "/on_show", method = RequestMethod.GET)
    public RestResult getOnShow(HttpServletRequest request, HttpServletResponse response) {
        LOG.info(request.getMethod() + " " + request.getRequestURI());
        return new RestResult(movieService.getMovieByStatus("on"));
    }

    @RequestMapping(path = "/coming_soon", method = RequestMethod.GET)
    public RestResult getComingSoon(HttpServletRequest request, HttpServletResponse response) {
        LOG.info(request.getMethod() + " " + request.getRequestURI());
        return new RestResult(movieService.getMovieByStatus("soon"));
    }

    @RequestMapping(path = "/popular", method = RequestMethod.GET)
    public RestResult getPopular(@RequestParam(value="count", defaultValue="3") int count,
                                 HttpServletRequest request, HttpServletResponse response) {
        LOG.info(request.getMethod() + " " + request.getRequestURI());
        RestResult result = new RestResult();
        result.put("count", count);
        // TODO GET /resource/movie/popular?count=XXX
        return result;
    }
}

@@ -459,16 +459,45 @@
 ashMap;%0A
+import java.util.ArrayList;%0A%0A
 %0A/**%0A * 
@@ -1487,32 +1487,34 @@
 URI());%0A        
+//
 return new RestR
@@ -1553,24 +1553,260 @@
 tus(%22on%22));%0A
+        RestResult result = new RestResult();%0A        // TODO GET /resource/movie/on_show%0A        result.put(%22count%22,9);%0A        int%5B%5D array = %7B1, 4, 5, 8, 9, 10, 11, 15, 16%7D;%0A        result.put(%22movies%22, array);%0A        return result;%0A
     %7D%0A%0A    @
@@ -2038,32 +2038,34 @@
 URI());%0A        
+//
 return new RestR
@@ -2106,24 +2106,217 @@
 s(%22soon%22));%0A
+        RestResult result = new RestResult();%0A        result.put(%22count%22,9);%0A        int%5B%5D array = %7B2, 3, 6, 7, 12, 13, 14, 17, 18%7D;%0A        result.put(%22movies%22, array);%0A        return result;%0A
     %7D%0A%0A    @
@@ -2768,16 +2768,743 @@
 unt=XXX%0A
+        ArrayList%3CObject%3E data = new ArrayList%3CObject%3E();%0A        LinkedHashMap%3CString, Object%3E map1 = new LinkedHashMap%3CString, Object%3E();%0A        map1.put(%22id%22,1);%0A        map1.put(%22posterURL%22,%22http://120.25.76.106:8080/poster/large/1.png%22);%0A        data.add(map1);%0A        LinkedHashMap%3CString, Object%3E map2 = new LinkedHashMap%3CString, Object%3E();%0A        map2.put(%22id%22,2);%0A        map2.put(%22posterURL%22, %22http://120.25.76.106:8080/poster/large/2.png%22);%0A        data.add(map2);%0A        LinkedHashMap%3CString, Object%3E map3 = new LinkedHashMap%3CString, Object%3E();%0A        map3.put(%22id%22,3);%0A        map3.put(%22posterURL%22, %22http://120.25.76.106:8080/poster/large/3.png%22);%0A        data.add(map3);%0A        result.put(%22subjects%22,data);%0A
         

add return data