package com.iformall.controller;

import com.alibaba.fastjson.JSONObject;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxQuestion;
import com.iformall.domain.po.WxQuestionConfig;
import com.iformall.domain.po.WxQuestionLog;
import com.iformall.enums.EnumQuestionConfigStatus;
import com.iformall.service.WxCUserBasicInfoService;
import com.iformall.service.WxCUserTagsService;
import com.iformall.service.WxQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.saxon.trans.Err;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/api/wxQuestion")
@Api(description="问券调查")
public class WxQuestionController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private WxQuestionService wxQuestionService;

	@Autowired
	private WxCUserTagsService wxCUserTagsService;

	@Autowired
	private WxCUserBasicInfoService wxCUserBasicInfoService;

    @ApiOperation("查寻问券")
    @GetMapping("getQuestion")
    public Result getQuestion(@RequestParam("couponType") String couponType) {
		WxQuestionConfig wxQuestionConfig = new WxQuestionConfig();
		wxQuestionConfig.setTenantId(getTenantId());
		JSONArray ct = new JSONArray();
		try {
			ct.put(Integer.valueOf(couponType));
		}catch (Exception e) {
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		}
		wxQuestionConfig.setCouponTypeList(ct.toString());
		List<WxQuestionConfig>  listConfig = wxQuestionService.findConfigList(wxQuestionConfig);
		if (listConfig.size() > 0) {
			wxQuestionConfig = listConfig.get(0);
			List<Long> questionIds = JSONObject.parseArray(wxQuestionConfig.getQuestionList(), Long.class);
			if (wxQuestionConfig.getStatus().equals(EnumQuestionConfigStatus.ON.getCode())
					&& questionIds.size() > 0) {
				WxQuestion wxQuestion = new WxQuestion();
				wxQuestion.setTenantId(getTenantId());
				wxQuestion.setIds(questionIds);
				List<WxQuestion>  listQuestion = wxQuestionService.findList(wxQuestion);
				if (listQuestion.size() > 0) {
					Iterator<WxQuestion> q = listQuestion.iterator();
					WxQuestionLog wxQuestionLog = new WxQuestionLog();
					while(q.hasNext()){
						WxQuestion x = q.next();
						wxQuestionLog.setQuestionId(x.getId());
						wxQuestionLog.setUserId(getUserId());
						wxQuestionLog.setTenantId(getTenantId());

						if(wxQuestionService.findLogList(wxQuestionLog).size()>0){
							q.remove();
						}
					}
					if (listQuestion.size() > 0 &&
							(new Random().nextInt(100) <  listConfig.get(0).getStrength()))
						return new ResultData(listQuestion.get(new Random().nextInt(listQuestion.size())));
				}
			}
		}
    	return new ResultData();
    }


	@ApiOperation("设置问券配置")
	@PostMapping("answerQuestion")
	public Result answerQuestion(@RequestBody WxQuestionLog wxQuestionLog) {
		if (wxQuestionLog == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
		if (wxQuestionLog.getQuestionId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
		if (wxQuestionLog.getAnswer() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);

		if (wxQuestionLog.getAnswer().isEmpty()) {
			wxQuestionLog.setAnswer(null);
		} else {
			String[] arys1 = wxQuestionLog.getAnswer().split(",");
			List<Long> as = new ArrayList<>();
			for (int i = 0; i < arys1.length; i++) {
				as.add(Long.parseLong(arys1[i]));
			}
			wxQuestionLog.setAnswer(JSONObject.toJSONString(as));

			wxCUserTagsService.assignTags(as, wxCUserBasicInfoService.getById(getUserId()));
		}

		wxQuestionLog.setUserId(getUserId());

		wxQuestionService.saveOrUpdateLog(wxQuestionLog);
		return new ResultData();
	}

}
