package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.file.ImageUtils;
import com.fs.course.domain.FsUserCourseFavorite;
import com.fs.course.param.FsUserCourseFollowUParam;
import com.fs.his.domain.*;
import com.fs.his.param.*;
import com.fs.his.service.*;
import com.fs.his.vo.FsDiseaseListUVO;
import com.fs.his.vo.FsDoctorListUVO;
import com.fs.his.vo.FsPackageDetailVO;
import com.fs.his.vo.FsPackageListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;


@Api("套餐接口")
@RestController
@RequestMapping(value = "/app/package")
public class PackageController extends AppBaseController {

    private static final Logger log = LoggerFactory.getLogger(PackageController.class);
    @Autowired
    private IFsPackageCateService packageCateService;
    @Autowired
    private IFsPackageService packageService;
    @Autowired
    private IFsPackageFavoriteService packageFavoriteService;
    @Autowired
    private IFsDoctorService doctorService;
    @Autowired
    private IFsStoreService storeService;

    @ApiOperation("获取套餐分类")
    @Cacheable(value = "getPackagCateList", key = "#param")
    @GetMapping("/getPackagCateList")
    public R getPackagCateList(FsPackageCateUParam param) {
        List<FsPackageCate> cates = packageCateService.selectFsPackageCateList(param);
        return R.ok().put("data", cates);
    }

    @ApiOperation("获取套餐列表")
    @Cacheable(value = "getPackageList", key = "#param")
    @GetMapping("/getPackageList")
    public R getPackageList(FsPackageListUParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsPackageListUVO> list = packageService.selectFsPackageListUVO(param);
        PageInfo<FsPackageListUVO> listPageInfo = new PageInfo<>(list);
        return R.ok().put("data", listPageInfo);
    }

    @ApiOperation("获取套餐详情")
//    @Cacheable(value = "getPackageById", key = "#packageId")
    @GetMapping("/getPackageById")
    public R getPackageById(@RequestParam("packageId") Long packageId) {
        FsPackage fsPackage = packageService.selectFsPackageByPackageId(packageId);
        String storeName = "";
        if (fsPackage != null && fsPackage.getStoreId() != null) {
            FsStore store = storeService.selectFsStoreByStoreId(fsPackage.getStoreId());
            if (store != null) {
                storeName = store.getFullName() != null ? store.getFullName() : "";
            }
        } else {
            log.info("zyp \n【套餐包不存在】:{}", packageId);
        }

        FsPackageDetailVO vo = new FsPackageDetailVO();
        BeanUtils.copyProperties(fsPackage, vo);
        String imgUrl = fsPackage.getImgUrl();
        if (StringUtils.isNotBlank(imgUrl)) {
            try {
                String s = packageService.convertImageToJpg(imgUrl, fsPackage.getPackageId());
                if (StringUtils.isNotBlank(s)) {
                    fsPackage.setImgUrl(s);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

        return R.ok().put("data", fsPackage).put("storeName", storeName);
//        return R.ok().put("data",fsPackage).put("storeName",storeName);
    }

    @ApiOperation("获取套餐下医生")
    @GetMapping("/getPackageDoctorList")
    public R getPackageDoctorList(FsDoctorListUParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsDoctorListUVO> list = doctorService.selectFsDoctorListUVO(param);
        PageInfo<FsDoctorListUVO> listPageInfo = new PageInfo<>(list);
        return R.ok().put("data", listPageInfo);
    }

    @Login
    @ApiOperation("获取是否收藏")
    @GetMapping("/checkFavorite")
    public R checkFavorite(@RequestParam("packageId") Long packageId, HttpServletRequest request) {
        String headValue = ServletUtils.getRequest().getHeader("APPToken");
        Claims claims = jwtUtils.getClaimByToken(headValue);
        if (claims != null) {
            String userId = claims.getSubject().toString();
            R r = packageFavoriteService.checkFavorite(packageId, Long.parseLong(userId));
            if (r.get("code").equals(200)) {
                return R.ok().put("isFavorite", 1);
            } else {
                return R.ok().put("isFavorite", 0);
            }
        } else {
            return R.error("未登录");
        }

    }

    @Login
    @ApiOperation("收藏")
    @PostMapping("/doFavorite")
    @Transactional
    @Synchronized
    public R doFavorite(@RequestBody FsPackageFavoriteUParam param, HttpServletRequest request) {
        R r = packageFavoriteService.checkFavorite(param.getPackageId(), Long.parseLong(getUserId()));
        if (r.get("code").equals(200)) {
            //取消收藏
            packageFavoriteService.deleteFavorite(param.getPackageId(), Long.parseLong(getUserId()));
            return R.ok("已取消收藏");
        } else {
            //收藏
            FsPackageFavorite map = new FsPackageFavorite();
            map.setPackageId(param.getPackageId());
            map.setUserId(Long.parseLong(getUserId()));
            map.setCreateTime(new Date());
            packageFavoriteService.insertFsPackageFavorite(map);
            return R.ok("已收藏");
        }
    }

}
