package io.github.rcarlosdasilva.weixin.core.registry;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;

import io.github.rcarlosdasilva.weixin.common.Convention;
import io.github.rcarlosdasilva.weixin.core.WeixinRegistry;
import io.github.rcarlosdasilva.weixin.core.cache.impl.AccessTokenCacheHandler;
import io.github.rcarlosdasilva.weixin.core.cache.impl.AccountCacheHandler;
import io.github.rcarlosdasilva.weixin.core.exception.InvalidAccountException;
import io.github.rcarlosdasilva.weixin.model.Account;
import io.github.rcarlosdasilva.weixin.model.OpenPlatform;

public class Registration implements Serializable {

  private static final long serialVersionUID = 5219550386903571660L;

  private static final Logger LOGGER = LoggerFactory.getLogger(WeixinRegistry.class);

  public static final Registration instance = new Registration();

  private Setting setting = new Setting();
  private OpenPlatform openPlatform;

  private Registration() {
  }

  public static Registration getInstance() {
    return instance;
  }

  public Setting getSetting() {
    return setting;
  }

  public void setSetting(Setting setting) {
    this.setting = setting;
  }

  public OpenPlatform getOpenPlatform() {
    return openPlatform;
  }

  public void setOpenPlatform(OpenPlatform openPlatform) {
    this.openPlatform = openPlatform;
  }

  public void addAccount(Account account) {
    if (Registration.accountExists(account.getKey())) {
      LOGGER.warn("尝试添加一个已经存在的公众号信息：[{}]，本方法无法覆盖已有公众号信息，请使用updateAccount", account.getKey());
      return;
    }

    if (verifyAccount(account)) {
      AccountCacheHandler.getInstance().put(account.getKey(), account);
    }
  }

  public void updateAccount(Account account) {
    if (verifyAccount(account)) {
      unregister(account.getKey());
      AccountCacheHandler.getInstance().put(account.getKey(), account);
    }
  }

  private boolean verifyAccount(Account account) {
    if (account == null || Strings.isNullOrEmpty(account.getAppId())) {
      throw new InvalidAccountException();
    }

    if (!account.isWithOpenPlatform() && Strings.isNullOrEmpty(account.getAppSecret())) {
      LOGGER.warn("未找到公众号的app_scret，该公众号将不被注册");
      return false;
    }
    if (account.isWithOpenPlatform() && Strings.isNullOrEmpty(account.getRefreshToken())) {
      LOGGER.warn("未找到开放平台授权方的刷新令牌authorizer_refresh_token，该公众号将不被注册");
      return false;
    }

    if (Strings.isNullOrEmpty(account.getKey())) {
      LOGGER.warn("未设置Account.key，将使用公众号的appid：[{}]作为默认key", account.getAppId());
      account.withKey(account.getAppId());
    }

    return true;
  }

  /**
   * 取消注册的公众号信息.
   * 
   * @param key
   *          注册时的key，或是公众号appid
   */
  public void unregister(final String key) {
    if (Strings.isNullOrEmpty(key)) {
      return;
    }

    final Account account = lookup(key);

    if (account != null) {
      final String realKey = account.getKey();
      AccountCacheHandler.getInstance().remove(realKey);
      AccessTokenCacheHandler.getInstance().remove(realKey);
      LOGGER.info("已取消注册公众号信息：[{}]", key);
    } else {
      LOGGER.warn("未找到可取消注册的公众号信息：[{}]", key);
    }
  }

  public void unregisterUnique() {
    unregister(Convention.DEFAULT_UNIQUE_WEIXIN_KEY);
  }

  private static boolean accountExists(String key) {
    return lookup(key) != null;
  }

  /**
   * 通过appId或原始ID获取账号配置信息.
   * 
   * @param key
   *          可以为公众号appId或者公众号原始ID，也可以是注册时的key
   * @return {@link Account}
   */
  public static Account lookup(String key) {
    Account account = AccountCacheHandler.getInstance().get(key);
    if (account != null) {
      return account;
    }

    return AccountCacheHandler.getInstance().lookup(Account.create(key));
  }

}

@@ -2466,32 +2466,215 @@
 n false;%0A    %7D%0A%0A
+    if (Strings.isNullOrEmpty(account.getMpId())) %7B%0A      LOGGER.warn(%22%E6%9C%AA%E8%AE%BE%E7%BD%AEAccount.mpId%EF%BC%8C%E5%AF%B9%E5%85%AC%E4%BC%97%E5%8F%B7appid: %5B%7B%7D%5D%E6%9D%A5%E8%AF%B4%EF%BC%8C%E5%BD%93%E5%BE%AE%E4%BF%A1%E9%80%9A%E7%9F%A5%E5%9B%9E%E8%B0%83%E6%88%96%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E4%B8%8D%E8%AE%BE%E7%BD%AE%E5%8F%AF%E8%83%BD%E4%BC%9A%E5%AF%BC%E8%87%B4%E6%97%A0%E6%B3%95%E6%AD%A3%E7%A1%AE%E6%89%BE%E5%88%B0%E5%AF%B9%E5%BA%94%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7%E4%BF%A1%E6%81%AF%22,%0A          account.getAppId());%0A    %7D%0A
     if (Strings.

修改 增加对公众号信息中mpid的检测