package cn.pluss.platform.controller;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.appGuide.AppGuideService;
import cn.pluss.platform.entity.AppGuide;
import cn.pluss.platform.enums.image;
import com.alipay.api.domain.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author djh
 * 简介富文本内容
 */
@RestController
@RequestMapping("/intro")
public class IntroduceController {

    @Autowired
    private AppGuideService appGuideService;

    @GetMapping({"/common", "/common/{code}"})
    public Result<String> billIntro(@PathVariable(value = "code", required = false) String code, HttpServletRequest httpServletRequest) {
        if (StringUtils.isEmpty(code)) {
            String html = "<p><img src=\"https://www.shouyinbei.net/resources/images/upload/82031620725808697.jpg\" style=\"\"/></p><p><img src=\"https://www.shouyinbei.net/resources/images/upload/86751620725811158.jpg\" style=\"\"/></p><p><img src=\"https://www.shouyinbei.net/resources/images/upload/38401620725814105.jpg\" style=\"\"/></p><p><img src=\"https://www.shouyinbei.net/resources/images/upload/14561620725816223.jpg\" style=\"\"/></p><p><img src=\"https://www.shouyinbei.net/resources/images/upload/45071620725822267.jpg\" style=\"\"/></p><p><img src=\"https://www.shouyinbei.net/resources/images/upload/31221620725827105.jpg\" style=\"\"/></p><p><img src=\"https://www.shouyinbei.net/resources/images/upload/68211620725829889.jpg\" style=\"\"/></p><p><br/></p>";
            return ResultGenerator.genSuccessResult(html);
        }

        AppGuide entity = appGuideService.getByCode(code);
        if (!"SHTGKT".equals(code)){
            return ResultGenerator.genSuccessResult(entity == null ? "" : entity.getContent());
        }
        //如果是推广宽图则看宽图是否开放
        if (Objects.equals(entity.getType(), "1")) {
            String type = httpServletRequest.getHeader("type");
            //TODO 双端开启
            if ("1".equals(type) || "2".equals(type)){
                return ResultGenerator.genSuccessResult(entity.getContent());
            }
//            else if ("2".equals(type)){
//                return ResultGenerator.genFailResult("");
//            }
            return ResultGenerator.genSuccessResult(entity == null ? "" : entity.getContent());
        }else {
            return ResultGenerator.genFailResult("");
        }
    }
    private String image(String typeCode, String merchantType){
        if ("AG".equals(typeCode) || "FB".equals(typeCode) || "SB".equals(typeCode)){
            return image.PROMOTION_IMAGE.getImageUrl();
        }
        if ("1".equals(merchantType)){
            return image.SMALL_IMAGE.getImageUrl();
        }
        return image.OTHER_IMAGE.getImageUrl();
    }

    @PostMapping ("/common")
    public Result<String> billIntro(HttpServletRequest httpServletRequest,
                                    @RequestBody Map<String, String> params) {
        AppGuide entity = appGuideService.getByCode(params.get("code"));
        if (!"SHTGKT".equals(params.get("code"))){
            return ResultGenerator.genSuccessResult(entity == null ? "" : entity.getContent());
        }
        //如果是推广宽图则看宽图是否开放
        if (Objects.equals(entity.getType(), "1")) {
            String type = httpServletRequest.getHeader("type");
            //TODO 双端开启
            if ("1".equals(type)||"2".equals(type)) {
                return ResultGenerator.genSuccessResult(image(params.get("typeCode"), params.get("merchantType")));
            }
//            }else if ("2".equals(type)){
//                return ResultGenerator.genFailResult("");
//            }
            return ResultGenerator.genSuccessResult(entity == null ? "" : entity.getContent());
        }else {
            return ResultGenerator.genFailResult("");
        }
    }

    @PostMapping ("/commonV2")
    public Result<String> billIntroV2(HttpServletRequest httpServletRequest,
                                    @RequestBody Map<String, String> params) {
        AppGuide entity = appGuideService.getByCode(imageV2(params.get("typeCode"), params.get("merchantType")));

        //如果是推广宽图则看宽图是否开放
        if (Objects.equals(entity.getType(), "1")) {
            //获取安卓或者IOS
            String type = httpServletRequest.getHeader("type");
            //安卓
            if ("1".equals(type)){
                if ("1".equals(entity.getAndroid())) {
                    return ResultGenerator.genSuccessResult(entity.getContent());
                }
                return ResultGenerator.genSuccessResult("");
            //IOS
            }else if ("2".equals(type)){
                if ("1".equals(entity.getIos())) {
                    return ResultGenerator.genSuccessResult(entity.getContent());
                }
                return ResultGenerator.genSuccessResult("");
            }
            return ResultGenerator.genFailResult("");
        }else {
            return ResultGenerator.genFailResult("");
        }
    }

    private String imageV2(String typeCode, String merchantType){
        if ("AG".equals(typeCode) || "FB".equals(typeCode) || "SB".equals(typeCode)){
            return "SHTGKT";
        }
        if ("1".equals(merchantType)){
            return "XWTG";
        }
        return "PUSH";
    }

}