package com.wechat.callback.handlers.event;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wechat.callback.handlers.ITencentRequestHandler;
import com.wechat.callback.passiveresponses.BasePassiveResponse;
import com.wechat.callback.passiveresponses.TextResponse;
import com.wechat.callback.processors.ClickEventProcessor;
import com.wechat.callback.tencentrequests.events.ClickEventRequest;
import com.wechat.common.utils.XmlUtils;

/**
 * 按钮处理类
 */

@Component("clickEvent")
public class ClickReceivedHandler implements ITencentRequestHandler {
    @Autowired
    ClickEventProcessor clickProcessor;
    
    //log
    protected static final Log log = LogFactory.getLog(ClickReceivedHandler.class);

    @Override
    public BasePassiveResponse handle(String xml, Object obj) {
        try {
            ClickEventRequest req = XmlUtils.xml2obj(xml, ClickEventRequest.class);
            if (req == null) {
                throw new RuntimeException("Xml parse error");
            }

            if (req.getEventKey() == null) {
                log.warn("点击事件类别为空!");
                return BasePassiveResponse.NULL_RESPONSE;
            }

            switch (req.getEventKey()) {
                //我的账户
                case CALL_ACCOUNT_INQUIRY: {
                    TextResponse response = clickProcessor.getAccountBinding(req);
                    return response;
                }

                default: {
                    log.warn("未定义的点击事件:[ " + req.getEventKey() + "]");
                    return BasePassiveResponse.NULL_RESPONSE;
                }
            }
        } catch (Exception ex) {
            log.error("点击事件分发处理异常");
            log.error(ex);
            return TextResponse.SERVER_ERROR();
        }
    }
}
