/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.mp.comment.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.config.WxConfig;
import com.yami.shop.mp.comment.model.WeiXinMenu;
import com.yami.shop.mp.comment.service.WeixinMenuService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 *
 * @author lgh on 2019/05/13.
 */
@RestController
@RequestMapping("/weixin/weixinMenu")
public class WeixinMenuController {

    @Autowired
    private WeixinMenuService weixinMenuService;
	@Autowired
	private WxConfig wxConfig;
	/**
	 * 分页获取
	 */
    @GetMapping("/page")
	//@PreAuthorize("@pms.hasPermission('weixin:weiXinMenu:page')")
	public ResponseEntity<IPage<WeiXinMenu>> page(WeiXinMenu weiXinMenu,PageParam<WeiXinMenu> page){
		IPage<WeiXinMenu> list = weixinMenuService.page(page);
		return ResponseEntity.ok(list);
	}





    /**
	 * 获取信息
	 */
	@GetMapping("/info")
	//@PreAuthorize("@pms.hasPermission('weixin:weiXinMenu:info')")
	public ResponseEntity<String> info(){
		WeiXinMenu weiXinMenu = weixinMenuService.getById(1);
		//如果为空则创建一个
		if(weiXinMenu == null){
			weiXinMenu =new WeiXinMenu();
			weiXinMenu.setId(1);
			weiXinMenu.setMenu("{ \"button\": [] }");
			weiXinMenu.setUpdateTime(new Date());
			weixinMenuService.save(weiXinMenu);
		}
		return ResponseEntity.ok(weiXinMenu.getMenu());
	}

	/**
	 * 保存
	 */
	@PostMapping
//	@PreAuthorize("@pms.hasPermission('weixin:weiXinMenu:save')")
	public ResponseEntity<Void> save(@RequestBody @Valid WeiXinMenu weiXinMenu){
		weixinMenuService.save(weiXinMenu);
		return ResponseEntity.ok().build();
	}

	/**
	 * 修改
	 */
	@PutMapping
	//@PreAuthorize("@pms.hasPermission('weixin:weiXinMenu:update')")
	public ResponseEntity<Void> update(@RequestBody @Valid WeiXinMenu weiXinMenu){
		System.err.println(weiXinMenu.getMenu());
		weiXinMenu.setId(1);
		weiXinMenu.setUpdateTime(new Date());
		weiXinMenu.setMenu(weiXinMenu.getMenu());
		weixinMenuService.updateById(weiXinMenu);
		return ResponseEntity.ok().build();
	}


	/**
	 * 发布
	 */
	@PutMapping("release")
	//@PreAuthorize("@pms.hasPermission('weixin:weiXinMenu:update')")
	public ResponseEntity<Void> release(){
		try {
			//获取
			WeiXinMenu weiXinMenu = weixinMenuService.getById(1);
			//对接微信接口发布菜单
			WxMpMenuService menuService = wxConfig.getWxMpService().getMenuService();
			menuService.menuDelete();
			menuService.menuCreate(weiXinMenu.getMenu());
		} catch (WxErrorException e) {
			e.printStackTrace();
			throw  new YamiShopBindException("错误: Code=" + e.getError().getErrorCode() + ", Msg=" + e.getError().getErrorMsg() );
		}
		return ResponseEntity.ok().build();
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/{id}")
	//@PreAuthorize("@pms.hasPermission('weixin:weiXinMenu:delete')")
	public ResponseEntity<Void> delete(@PathVariable Long id){
		weixinMenuService.removeById(id);
		return ResponseEntity.ok().build();
	}
}
