package com.ywt.mg.web.interceptors;

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

/**
 * 保存当前请求与业务相关的信息上下文
 * 由拦截器层提供信息初始化
 * Created by huangguoping.
 */
public class WebAppContext {
    private volatile 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<Integer> _adminid = new ThreadLocal<>();
    private ThreadLocal<Integer> _hospitalId = new ThreadLocal<>();
    private ThreadLocal<Integer> _enterpriseId = new ThreadLocal<>();
    private ThreadLocal<Integer> _shopId = new ThreadLocal<>();
    private ThreadLocal<Integer> _terminal = new ThreadLocal<>();
    private ThreadLocal<Integer> _pharmacyId = 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();
    }

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

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

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

    public String getIP(){
        return  _ip.get() == null ? "" : _ip.get();
    }
    protected WebAppContext setIP(String ip){
        _ip.set(ip);
        return this;
    }
    public Map<String, Cookie> getCookies(){
        return _cookies.get();
    }

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

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

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

    protected 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 HttpSession getSession(){
        return this.sessionThreadLocal.get();
    }

    protected WebAppContext setAdminId(int adminid){
        this._adminid.set(adminid);
        return this;
    }

    public Integer getAdminId(){
        return this._adminid.get();
    }

    protected WebAppContext setHospitalId(int hospitalId){
        this._hospitalId.set(hospitalId);
        return this;
    }
    public Integer getHospitalId(){
        return this._hospitalId.get();
    }

    protected WebAppContext setEnterpriseId(int enterpriseId){
        this._enterpriseId.set(enterpriseId);
        return this;
    }

    public Integer getEnterpriseId() {
        return this._enterpriseId.get();
    }

    protected WebAppContext setShopId(int shopId){
        this._shopId.set(shopId);
        return this;
    }

    public Integer getShopId(){return this._shopId.get();}


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

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

    protected WebAppContext setPharmacyId(int id){
        this._pharmacyId.set(id);
        return this;
    }

    public Integer getPharmacyId(){
        return this._pharmacyId.get();
    }

    protected void init(){
        _capChecked.set(false);
        _requestStartTime.set(0L);
        _requestID.set("");
        _ip.set("");
        _cookies.set(null);
        _host.set("");
        _token.set("");
        sessionThreadLocal.set(null);
        _adminid.set(0);
        _hospitalId.set(0);
        _enterpriseId.set(0);
        _shopId.set(0);
        _terminal.set(0);
        _pharmacyId.set(0);
    }

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