package com.yc.education.controller.admin;

import com.yc.education.model.Prize;
import com.yc.education.model.User;
import com.yc.education.service.PrizeService;
import com.yc.education.service.UserService;
import com.yc.education.util.DrawUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class UserController {
//extends BaseController

    @Autowired
    UserService userService;

    static final int TIME = 100000;

    @Autowired
    PrizeService prizeService;

    // 用来产生随机验证码的
    private static Random rand = new Random();

    private static String[] str = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    @RequestMapping("index.html")
    public ModelAndView adminlogin() {
        return new ModelAndView();
    }


    @RequestMapping("draw.html")
    public ModelAndView index(HttpServletResponse response, HttpServletRequest request, User user) {
        ModelAndView result = new ModelAndView();
        //判断管理员时候登录 没有登录定向到登录页面
        User finduserlogin = userService.finduser(user.getUphone(), user.getUemil());
        System.out.println(finduserlogin);
        if (finduserlogin != null && finduserlogin.getUstate() == 0) {
            List<User> userAllBysturt = userService.getUserAllBysturt();
            for (User user1 : userAllBysturt) {
                StringBuffer stringBuffer = new StringBuffer(user1.getUphone());
                stringBuffer.replace(3, 7, "****");
                user1.setUphone(stringBuffer.toString());
            }
            List<Prize> prizeList = prizeService.getAll();

            request.getSession().setAttribute("user", finduserlogin);
            request.getSession().setAttribute("json", prizeList.toArray());
            request.getSession().setAttribute("userAll", userAllBysturt);
            result.setViewName("draw");
        } else {
            result.setViewName("index");
        }
        return result;
    }

    @RequestMapping("add")
    public void ajax(HttpServletResponse httpServletResponse, HttpServletRequest request, String uremark, String id) throws IOException {
        if (uremark != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            User user = (User) request.getSession().getAttribute("user");
            user.setUstate(1);
            user.setuDate(df.format(new Date()));
            user.setUremark(uremark);
            userService.updateUser(user);
            prizeService.updatePrize(uremark);
        }
    }

    @ResponseBody
    @RequestMapping("prize")
    public void ajax1(HttpServletResponse httpServletResponse, HttpServletRequest request) throws IOException {
        List<Prize> all = prizeService.getAll();
        request.getSession().setAttribute("prize", all);
        JSONArray array = JSONArray.fromObject(all);
        PrintWriter writer = httpServletResponse.getWriter();
        writer.print(array);
        writer.flush();
        writer.close();
    }

    @RequestMapping("results")
    public String results(HttpServletRequest request) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        User user1 = userService.selectUserById(user.getUid());
        request.getSession().setAttribute("user", user1);
        return "results";
    }

    @ResponseBody
    @RequestMapping("gailv")
    public void gailv(HttpServletResponse httpServletResponse) throws IOException {
        List<Prize> all = prizeService.getAll();
        DrawUtil drawUtil = new DrawUtil();
        int i1 = drawUtil.drawGift(all) + 1;
        PrintWriter writer = httpServletResponse.getWriter();
        writer.print(i1);
        writer.flush();
        writer.close();
    }


}
