package com.ywt.outpatient.core;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 保存当前请求与业务相关的信息上下文
 * 由拦截器层提供信息初始化
 * Created by huangguoping.
 */
public class WebAppContext {
    private static WebAppContext _webAppContext = null;
    private static Object _lockObj = new Object();

    private ThreadLocal<Boolean> _capChecked = new ThreadLocal<>();
    private ThreadLocal<Long> _requestStartTime = new ThreadLocal<>();
    private ThreadLocal<String> _requestID = new ThreadLocal<>();
    private ThreadLocal<String> _ip = new ThreadLocal<>();
    private ThreadLocal<Map<String, Cookie>> _cookies = new ThreadLocal<>();
    private ThreadLocal<String> _host = new ThreadLocal<>();
    private ThreadLocal<String> _token = new ThreadLocal<>();
    private ThreadLocal<HttpSession> sessionThreadLocal = new ThreadLocal<>();
    private ThreadLocal<String> _tokenid = new ThreadLocal<>();
    private ThreadLocal<String> _unionId = new ThreadLocal<>();
    private ThreadLocal<String> _openId = new ThreadLocal<>();
    private ThreadLocal<Integer> _userId = new ThreadLocal<>();
    private ThreadLocal<Integer> _doctorId = new ThreadLocal<>();
    private ThreadLocal<String> _doctorName = new ThreadLocal<>();
    private ThreadLocal<Integer> _terminal = new ThreadLocal<>();
    private ThreadLocal<String> _gwProtocol = new ThreadLocal<>();
    private ThreadLocal<String> _gwAuthAppid = new ThreadLocal<>();


    private WebAppContext() {
    }

    public boolean capChecked() {
        return _capChecked.get() == null ? false : _capChecked.get();
    }

    protected WebAppContext setCapChecked(boolean flag) {
        _capChecked.set(flag);
        return this;
    }

    public long getRequestStartTime() {
        return _requestStartTime.get() == null ? 0L : _requestStartTime.get();
    }

    public WebAppContext setRequestStartTime(long time) {
        _requestStartTime.set(time);
        return this;
    }

    public String getRequestID() {
        return _requestID.get() == null ? "" : _requestID.get();
    }

    public WebAppContext setRequestID(String id) {
        _requestID.set(id);
        return this;
    }

    public String getIP() {
        return _ip.get() == null ? "" : _ip.get();
    }

    public WebAppContext setIP(String ip) {
        _ip.set(ip);
        return this;
    }

    public Map<String, Cookie> getCookies() {
        return _cookies.get();
    }

    public WebAppContext setCookies(Map<String, Cookie> cookies) {
        _cookies.set(cookies);
        return this;
    }

    public String getHost() {
        return this._host.get();
    }

    public WebAppContext setHost(String host) {
        this._host.set(host);
        return this;
    }

    public WebAppContext setToken(String token) {
        this._token.set(token);
        return this;
    }

    public String getToken() {
        return this._token.get();
    }

    protected WebAppContext setSession(HttpSession session) {
        this.sessionThreadLocal.set(session);
        return this;
    }

    public String getUnionId() {
        return _unionId.get() == null ? "" : _unionId.get();
    }

    protected void setUnionId(String unionId){
        _unionId.set(unionId);
    }

    public String getOpenId() {
        return _openId.get() == null ? "" : _openId.get();
    }

    protected void setOpenId(String openId){
        _openId.set(openId);
    }

    public Integer getUserId() {
        return _userId.get() == null ? 0 : _userId.get();
    }

    protected void setUserId(Integer userId){
        _userId.set(userId);
    }

    public Integer getDoctorId(){
        return _doctorId.get() == null ? 0 : _doctorId.get();
    }

    public void setDoctorId(Integer doctorId){
        _doctorId.set(doctorId);
    }

    public String getTokenID() {
        return _tokenid.get() == null ? "" : _tokenid.get();
    }

    protected void setTokenID(String tokenID){
        _tokenid.set(tokenID);
    }

    public String getDoctorName(){
        return _doctorName.get() == null ? "" : _doctorName.get();
    }

    protected void setDoctorName(String doctorName){
        _doctorName.set(doctorName);
    }

    public Integer getTerminal() {
        return _terminal.get() == null ? 0 : _terminal.get();
    }

    protected void setTerminal(Integer terminal) {
        this._terminal.set(terminal);
    }

    public String getGWProtocol() {
        return _gwProtocol.get() == null ? "" : _gwProtocol.get();
    }

    protected void setGWProtocol(String protocol){
        _gwProtocol.set(protocol);
    }

    protected void setGWAuthAppid(String appid){
        this._gwAuthAppid.set(appid);
    }

    public String getGWAuthAppid(){
        return this._gwAuthAppid.get();
    }


    public void init() {
        _capChecked.set(false);
        _requestStartTime.set(0L);
        _requestID.set("");
        _ip.set("");
        _cookies.set(null);
        _host.set("");
        _token.set("");
        sessionThreadLocal.set(null);
        _tokenid.set("");
        _unionId.set("");
        _openId.set("");
        _userId.set(0);
        _doctorId.set(0);
        _doctorName.set("");
        _terminal.set(0);
        _gwProtocol.set("");
        _gwAuthAppid.set("");
    }

    public static WebAppContext current() {
        if (_webAppContext == null) {
            synchronized (_lockObj) {
                if (_webAppContext == null) {
                    _webAppContext = new WebAppContext();
                }
            }
        }
        return _webAppContext;
    }
}
