package cn.com.qmth.stmms.biz.config.service.impl;

import java.util.List;

import javax.annotation.PostConstruct;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.com.qmth.stmms.biz.config.model.SystemAuth;
import cn.com.qmth.stmms.biz.config.service.SystemAuthService;
import cn.com.qmth.stmms.biz.config.service.SystemConfigService;
import cn.com.qmth.stmms.biz.school.model.School;
import cn.com.qmth.stmms.biz.school.service.SchoolService;
import cn.com.qmth.stmms.biz.utils.AppLicenseUtil;
import cn.com.qmth.stmms.biz.utils.SolarHttpUtil;
import cn.com.qmth.stmms.common.enums.ConfigType;
import cn.com.qmth.stmms.common.enums.SystemAuthType;
import cn.com.qmth.stmms.common.utils.AesUtils;

import com.qmth.boot.tools.models.ByteArray;

@Component
public class SystemCache {

    @Autowired
    private SystemAuthService authService;

    @Autowired
    private SystemConfigService configService;

    @Autowired
    private SchoolService schoolService;

    private boolean Auth;

    private boolean doubleTrack;

    private boolean groupDeleteWarn;

    private Long expireTime;

    private String fileServer;

    private Long markTime;

    @Value("${qmth.solar.host}")
    private String host;

    @Value("${qmth.solar.app.uri}")
    private String uri;

    @PostConstruct
    public void init() {
        SystemAuth s = authService.findOne();
        if (s != null && SystemAuthType.ONLINE.equals(s.getType())) {
            String accessKey = AesUtils.decrypt(s.getAccessKey());
            String accessSecret = AesUtils.decrypt(s.getAccessSecret());
            SolarHttpUtil httpUtil = new SolarHttpUtil(accessKey, accessSecret, host, uri);
            if (httpUtil.getAPP()) {
                Auth = true;
                doubleTrack = true;
                groupDeleteWarn = false;
            }
        } else if (s != null && SystemAuthType.OFFLINE.equals(s.getType())) {
            JSONObject json = AppLicenseUtil.parseLicense(ByteArray.fromBase64(s.getDescription()).value());
            this.parseJson(json);
        } else {
            Auth = false;
            doubleTrack = false;
            groupDeleteWarn = false;
        }
        fileServer = configService.findByType(ConfigType.FILE_SERVER);
        markTime = Long.parseLong(configService.findByType(ConfigType.MARK_TIME));
    }

    public boolean isAuth() {
        return Auth;
    }

    public void setAuth(boolean auth) {
        Auth = auth;
    }

    public String getFileServer() {
        return fileServer;
    }

    public void setFileServer(String fileServer) {
        this.fileServer = fileServer;
    }

    public Long getMarkTime() {
        return markTime;
    }

    public void setMarkTime(Long markTime) {
        this.markTime = markTime;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public boolean isDoubleTrack() {
        return doubleTrack;
    }

    public void setDoubleTrack(boolean doubleTrack) {
        this.doubleTrack = doubleTrack;
    }

    public boolean isGroupDeleteWarn() {
        return groupDeleteWarn;
    }

    public void setGroupDeleteWarn(boolean groupDeleteWarn) {
        this.groupDeleteWarn = groupDeleteWarn;
    }

    public void parseJson(JSONObject json) {
        if (json != null) {
            Auth = true;
            List<School> list = schoolService.parseJson(json.getString("orgs"));
            schoolService.updateOrg(list);
            if (json.has("control") && json.getJSONObject("control").has("expireTime")) {
                Long expireTime = null;
                try {
                    expireTime = json.getJSONObject("control").getLong("expireTime");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                this.expireTime = expireTime;
            }
            this.doubleTrack = false;
            if (json.has("control") && json.getJSONObject("control").has("custom")
                    && json.getJSONObject("control").getJSONObject("custom").has("enable_double_track")) {
                try {
                    this.doubleTrack = json.getJSONObject("control").getJSONObject("custom")
                            .getBoolean("enable_double_track");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.groupDeleteWarn = false;
            if (json.has("control") && json.getJSONObject("control").has("custom")
                    && json.getJSONObject("control").getJSONObject("custom").has("enable_group_delete_warn")) {
                try {
                    this.groupDeleteWarn = json.getJSONObject("control").getJSONObject("custom")
                            .getBoolean("enable_group_delete_warn");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            Auth = false;
            doubleTrack = false;
            groupDeleteWarn = false;
            expireTime = null;
        }
    }

}
