package cn.hmsoft.weixin.control;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.control.FrameControl;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.JsonHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.weixin.constants.WxMenuType;
import cn.hmsoft.weixin.constants.WxResId;
import cn.hmsoft.weixin.data.dao.WxMenuDefineDao;
import cn.hmsoft.weixin.data.model.WxMenuDefine;
import cn.hmsoft.weixin.entity.request.MenuCreateRequestJson;
import cn.hmsoft.weixin.entity.response.MenuAddconditionalResponseJson;
import cn.hmsoft.weixin.entity.response.MenuGetResponseJson;
import cn.hmsoft.weixin.request.MenuAddconditionalRequest;
import cn.hmsoft.weixin.request.MenuDeleteRequest;
import cn.hmsoft.weixin.request.MenuDeleteconditionalRequest;
import cn.hmsoft.weixin.request.MenuGetRequest;
import cn.hmsoft.weixin.request.MenuGreateRequest;
import cn.hmsoft.weixin.service.WxMenuService;

@RestController
public class WxMenuControl extends FrameControl {
	@Autowired
	private WxMenuDefineDao daoMenu;
	@Autowired
	private WxMenuService sMenu;

	@RequestMapping("weixin/menu/page")
	public Ajax page(Integer wx_id) {
		if (wx_id == null)
			return new Ajax(new Pager(new ArrayList<String>()));
		return new Ajax(new Pager(this.daoMenu.listMenu(wx_id)));
	}

	@RequestMapping("weixin/menu/save")
	public Ajax save(int wx_id, WxMenuDefine menu) {
		sMenu.saveMenu(wx_id, menu, this.getFrameOptr());
		return new Ajax();

	}

	@RequestMapping("weixin/menu/reset")
	public Ajax reset(int wx_id) {
		sMenu.resetMenu(wx_id, this.getFrameOptr());
		return new Ajax();

	}

	@RequestMapping("weixin/menu/fetch")
	public Ajax fetch(int wx_id) {
		this.authOptrRole(WxResId.Menu_Manage);
		MenuGetRequest mr = new MenuGetRequest();
		mr.setWxDefine(wx_id);
		MenuGetResponseJson json = mr.request();
		//
		// MenuCreateRequestJson rJson = new MenuCreateRequestJson();
		// List<MenuCreateRequestJson.FunButton> list = new
		// ArrayList<MenuCreateRequestJson.FunButton>();
		// for (MenuGetResponseJson.FunButton fn : json.getMenu().getButton()) {
		// MenuCreateRequestJson.FunButton btn = rJson.new FunButton();
		// btn.setMedia_id(fn.getMedia_id());
		// btn.setKey(fn.getKey());
		// btn.setName(fn.getName());
		// btn.setType(fn.getType());
		// btn.setUrl(fn.getUrl());
		// btn.setSub_button(new ArrayList<MenuCreateRequestJson.FunButton>());
		// for (MenuGetResponseJson.FunButton f : fn.getSub_button()) {
		// MenuCreateRequestJson.FunButton bn = rJson.new FunButton();
		// bn.setMedia_id(f.getMedia_id());
		// bn.setKey(f.getKey());
		// bn.setName(f.getName());
		// bn.setType(f.getType());
		// bn.setUrl(f.getUrl());
		// btn.getSub_button().add(bn);
		//
		// }
		// list.add(btn);
		// }
		// rJson.setButton(list);
		return new Ajax(json);

	}

	@RequestMapping("weixin/menu/make")
	public Ajax make(int wx_id) {
		MenuCreateRequestJson json = new MenuCreateRequestJson();
		List<MenuCreateRequestJson.FunButton> list = new ArrayList<MenuCreateRequestJson.FunButton>();
		String sql = "select * from wx_menu_define where menu_status=? and menu_pid=? and wx_id=?";
		for (WxMenuDefine menu : this.daoMenu.listBySql(WxMenuDefine.class, sql, FrameStatus.Active.toString(), "K00",
				wx_id)) {
			MenuCreateRequestJson.FunButton fBtn = json.new FunButton();
			fBtn.setKey(menu.getMenu_key());
			fBtn.setName(menu.getMenu_text());
			fBtn.setType(menu.getMenu_type());
			if (menu.getMenu_type().equals(WxMenuType.View))
				fBtn.setUrl(menu.getMenu_param());
			fBtn.setSub_button(new ArrayList<MenuCreateRequestJson.FunButton>());
			for (WxMenuDefine mu : this.daoMenu.listBySql(WxMenuDefine.class, sql, FrameStatus.Active.toString(),
					menu.getMenu_key(), wx_id)) {
				MenuCreateRequestJson.FunButton fb = json.new FunButton();
				fb.setKey(mu.getMenu_key());
				fb.setName(mu.getMenu_text());
				fb.setType(mu.getMenu_type());
				if (StringHelper.isNotEmpty(mu.getMenu_param())) {
					if (mu.getMenu_type().equals(WxMenuType.View))
						fb.setUrl(mu.getMenu_param());
					else
						fb.setMedia_id(mu.getMenu_param());
				}
				fBtn.getSub_button().add(fb);
			}
			list.add(fBtn);

		}
		json.setButton(list);

		return new Ajax(json);

	}

	@RequestMapping("weixin/menu/upload")
	public Ajax upload(int wx_id, String json_value) {
		FrameAssertUtil.isNotEmpty(json_value, "JSON字符串为空");
		this.authOptrRole(WxResId.Menu_Manage);

		MenuCreateRequestJson json = null;
		try {
			json = JsonHelper.toObject(json_value, MenuCreateRequestJson.class);
		} catch (Exception e) {
			throw new BusinessException("JSON字符串错误");
		}
		if (json == null || json.getButton() == null || json.getButton().size() < 1)
			throw new BusinessException("未发现任何菜单信息，如需取消菜单请使用删除菜单接口");
		if (json.getMatchrule() == null) {
			MenuGreateRequest rq = new MenuGreateRequest(json);
			rq.setWxDefine(wx_id);
			rq.request();
		} else {
			MenuAddconditionalRequest rq = new MenuAddconditionalRequest(json);
			rq.setWxDefine(wx_id);
			MenuAddconditionalResponseJson j = rq.request();
			return new Ajax(j.getMenuid());
		}
		return new Ajax();

	}

	@RequestMapping("weixin/menu/delete")
	public Ajax delete(int wx_id, Integer menu_id) {
		this.authOptrRole(WxResId.Menu_Manage);
		if (menu_id == null || menu_id < 1) {
			MenuDeleteRequest mr = new MenuDeleteRequest();
			mr.setWxDefine(wx_id);
			mr.request();
		}else {
			//删除个性化菜单
			MenuDeleteconditionalRequest rq = new MenuDeleteconditionalRequest(menu_id);
			rq.setWxDefine(wx_id);
			rq.request();
		}
		
		return new Ajax();
	}
}
