package com.cab.controller;

import com.cab.model.CabLiveContent;
import com.cab.service.CabLiveContentServiceImpl;
import com.cku.core.PageBeanResult;
import com.cku.core.RESTResponse;
import com.cku.core.ZAException;
import com.cku.util.PageBean;
import com.cku.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by user on 2016/6/28.
 */
@Controller
@RequestMapping("live")
public class CabLiveContentController {
    @Autowired
    private CabLiveContentServiceImpl cabLiveContentService;

    @RequestMapping("/listContent")
    public ModelAndView listUser(HttpServletRequest request, HttpServletResponse response){
        PageBean pb = ServletUtils.getParameterBeanWith10(request);
        String contentTitle = ServletUtils.getParameter(request, "title",null);
        PageBeanResult<CabLiveContent> pbList = cabLiveContentService.selectAll(contentTitle,pb);
        return new ModelAndView("live/listContent").addObject("items",pbList);
    }


    @RequestMapping("/preAddContent")
    public ModelAndView preAddContent(HttpServletRequest request, HttpServletResponse response){
        ModelAndView _modelAndView = new ModelAndView("live/addContent");
        CabLiveContent cabLiveContent = new CabLiveContent();
        return _modelAndView.addObject("cabLiveContent", cabLiveContent);
    }

    @InitBinder
    public void InitBinder(ServletRequestDataBinder bin) {
        bin.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
    @RequestMapping("/addContent")
    public ModelAndView addContent(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, CabLiveContent cabLiveContent){
        cabLiveContent.setCreateDate(new Date());
        String checkResult = CabLiveContent.isBaseCheck(cabLiveContent);
        if (checkResult!=null) {
            String result = "error";
            modelMap.addAttribute("toPage","live/listContent.do");
            modelMap.addAttribute("message",checkResult);
            ModelAndView mv = new ModelAndView(result,modelMap);
            return  mv;
        }
        int i = cabLiveContentService.insertSelective(cabLiveContent);
        String videoUrl = cabLiveContent.getVideoUrl();
        Long contentId = (long)cabLiveContent.getContentId();
        Thread th = new Thread(new UploadFileToYun(contentId,cabLiveContentService,request.getRealPath("/uploadfile"),videoUrl.substring(videoUrl.lastIndexOf("/")+1, videoUrl.length())));
        th.start();
        String result = i>0?"success":"error";
        modelMap.addAttribute("toPage","live/listContent.do");
        modelMap.addAttribute("message",i>0?"添加视频成功!":"添加视频失败!");
        ModelAndView mv = new ModelAndView(result,modelMap);
        return mv;

    }

    @RequestMapping("/preUpdateContent")
    public ModelAndView preUpdateContent(HttpServletRequest request, HttpServletResponse response){
        ModelAndView _modelAndView = new ModelAndView("live/updateContent");
        try {
            Long id = ServletUtils.getParameterLong(request,"id");
            CabLiveContent cabLiveContent = cabLiveContentService.selectByPrimaryKey(id);
            _modelAndView.addObject("cabLiveContent", cabLiveContent);
        } catch (ZAException e) {
            e.printStackTrace();
        }

        return _modelAndView;
    }

    @RequestMapping("/updateContent")
    public ModelAndView updateContent(HttpServletRequest request, HttpServletResponse respons,CabLiveContent cabLiveContent,ModelMap modelMap){
        cabLiveContent.setCreateDate(new Date());
        String checkResult = CabLiveContent.isBaseCheck(cabLiveContent);
        if (checkResult!=null) {
            String result = "error";
            modelMap.addAttribute("toPage","live/listContent.do");
            modelMap.addAttribute("message",checkResult);
            ModelAndView mv = new ModelAndView(result,modelMap);
            return  mv;
        }
        int i = cabLiveContentService.updateByPrimaryKeySelective(cabLiveContent);
        String videoUrl = cabLiveContent.getVideoUrl();
        Thread th = new Thread(new UploadFileToYun((long)cabLiveContent.getContentId(),cabLiveContentService,request.getRealPath("/uploadfile"),videoUrl.substring(videoUrl.lastIndexOf("/")+1, videoUrl.length())));
        th.start();
        String result = i>0?"success":"error";
        modelMap.addAttribute("toPage","live/listContent.do");
        modelMap.addAttribute("message",i>0?"修改视频信息!":"修改视频失败!");
        ModelAndView mv = new ModelAndView(result,modelMap);
        return mv;
    }
    @RequestMapping("/deleteContent")
    @ResponseBody
    public void deleteContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RESTResponse result = null;
        try{
            Long id = ServletUtils.getParameterLong(request,"id");
            cabLiveContentService.deleteByPrimaryKey(id);
            result = new RESTResponse("items", "ok");
        }  catch (ZAException e) {
            e.printStackTrace();
        }
        ServletUtils.writeResponse(response, result);
    }
}
