package com.yonge.cooleshow.auth.middleware.wechat.domain;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import com.alibaba.fastjson.JSON;
import com.yonge.cooleshow.auth.api.entity.WxConfigInfo;
import com.yonge.cooleshow.auth.middleware.wechat.handler.WxMessageRouteEvent;
import lombok.Data;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;

import java.io.Serializable;

/**
 * Created by Eric.Shang on 4/3/17.
 */
@Data
public class WxConfigStorageWrapper implements Serializable {

    // 公众号
    private WxMpService wxMpService;
    private WxMpConfigStorage storage;
    private WxConfigInfo config;
    private WxMpMessageRouter router;

    public static WxConfigStorageWrapper build() {
        return new WxConfigStorageWrapper();
    }

    /*
    公共号配置
     */
    private WxMpConfigStorage wxMpConfigStorage() {
        // 默认实现方式，基于内存存储
        WxMpDefaultConfigImpl info = new WxMpDefaultConfigImpl();
        info.setAppId(getConfig().getAppid());
        info.setSecret(getConfig().getSecret());
        info.setAesKey(getConfig().getAeskey());
        info.setToken(getConfig().getToken());

        return info;
    }

    public WxConfigStorageWrapper config(String config) {

        this.config(JSON.parseObject(config, WxConfigInfo.class)).setStorage(wxMpConfigStorage());

        WxMpService wxService = new WxMpServiceImpl();
        wxService.setWxMpConfigStorage(getStorage());

        return this.wxMpService(wxService).router(WxMessageRouteEvent.router(wxService));
    }

    public WxConfigStorageWrapper config(WxConfigInfo config) {
        this.config = config;
        return this;
    }

    public WxConfigStorageWrapper wxMpService(WxMpService wxMpService) {
        this.wxMpService = wxMpService;
        return this;
    }

    public WxConfigStorageWrapper router(WxMpMessageRouter router) {
        this.router = router;
        return this;
    }

    /*
    小程序配置
     */
    private WxMaService wxMaService;
    private WxMaConfig maConfig;
    private WxMaMessageRouter maMessageRouter;

    private WxMaConfig wxMaConfigStorage() {
        // 小程序默认配置实现, 基于内存存储
        WxMaDefaultConfigImpl info = new WxMaDefaultConfigImpl();
        info.setAppid(getConfig().getAppid());
        info.setSecret(getConfig().getSecret());
        info.setAesKey(getConfig().getAeskey());
        info.setToken(getConfig().getToken());

        return info;
    }

    public WxConfigStorageWrapper maConfig(String config) {

        this.config(JSON.parseObject(config, WxConfigInfo.class)).setMaConfig(wxMaConfigStorage());

        WxMaService maService = new WxMaServiceImpl();
        maService.setWxMaConfig(getMaConfig());

        return this.wxMaService(maService).maMessageRouter(WxMessageRouteEvent.maRouter(maService));
    }


    public WxConfigStorageWrapper wxMaService(WxMaService wxMaService) {
        this.wxMaService = wxMaService;
        return this;
    }

    public WxConfigStorageWrapper maMessageRouter(WxMaMessageRouter maMessageRouter) {
        this.maMessageRouter = maMessageRouter;
        return this;
    }
}
