interface AccountPassword {
    account: string,
    password: string,
};

interface UserInfo {
    id: number,
    name: string,
    tenantId: string,
};

class LocalStorage {
    // 存储账号密码
    setAccountPassword = (accountPassword?: AccountPassword) => {
        if (accountPassword) {
            
            localStorage.setItem('accountPassword',  encodeURIComponent(JSON.stringify(accountPassword)));
        } else {
            localStorage.removeItem('accountPassword');
        }
    }

    // 存储凭证
    setToken = (token: string) => {
        localStorage.setItem('token', token);
    }

    // 存储用户信息
    setUserInfo = (info: UserInfo) => {
        localStorage.setItem('userInfo', JSON.stringify(info));
    }

    // 存储菜单类型
    setMenuType = (type: number) => {
        localStorage.setItem('menuType', String(type));
    }

    // 获取账号密码
    getAccountPassword = () => {
        const infoString = localStorage.getItem('accountPassword');

        if (infoString) {
            const info: AccountPassword = JSON.parse(decodeURIComponent(infoString));
            return info;
        } else {
            return undefined;
        }
    }

    // 获取凭证
    getToken = () => {
        const token = localStorage.getItem('token');

        if (token) {
            return token as string;
        } else {
            this.clear();
            return undefined;
        }
    }

    // 获取用户信息
    getUserInfo = () => {
        const infoString = localStorage.getItem('userInfo');

        if (infoString) {
            const info: UserInfo = JSON.parse(infoString);
            return info;
        } else {
            return undefined;
        }
    }

    // 获取菜单类型
    getMenuType = () => {
        const menuType = localStorage.getItem('menuType');

        if (menuType) {
            return Number(menuType);
        } else {
            return 0;
        }
    }

    // 清除
    clear = () => {
        /*
         * 保留密码
         */
        const accountPassword = this.getAccountPassword();
        const cloneAccountPassword = accountPassword ? { ...accountPassword } : undefined;

        // localStorage.clear();
        this.removeToken('token');
        this.removeToken('menuType');
        this.removeToken('permissions');
        this.removeToken('userInfo');
        this.removeToken('jk_constant_routes_v1');
        this.setAccountPassword(cloneAccountPassword);
    }
    // 移除token
    removeToken = (type: string) => {
        localStorage.removeItem(type);
    }

    // 存储roles
    setRoles = (roles: string) => {
        localStorage.setItem('roles', JSON.stringify(roles));
    }

    // 存储permissions
    setPermissions = (permissions: string) => {
        localStorage.setItem('permissions', JSON.stringify(permissions));
    }

    // 获取roles
    getRoles = (): string | null => {
        const rolesStr = localStorage.getItem('roles');
        return rolesStr;
    }

    // 获取permissions
    getPermissions = (): string | null => {
        const permissionsStr = localStorage.getItem('permissions');
        return permissionsStr;
    }

    getStatusFlag = (permissionsStr: string): boolean => {
        const permissions = this.getPermissions();
        if (permissions && (permissions.includes('*:*:*') || permissions.includes(permissionsStr))) {
            // admin
            return true;
        }
        return false;
    }

    getStatusRoleFlag = (): boolean => {
        const roles = this.getRoles();
        if (roles && (roles.includes('admin') || roles.includes('manager'))) {
            return true;
        } else {
            return false;
        }
    }
}

export default new LocalStorage();