/*
 * @Author: Kane
 * @Date: 2023-02-28 09:26:45
 * @LastEditors: Kane
 * @FilePath: /task_schedule/src/utils/api/localStorage.ts
 * @Description:
 *
 * Copyright (c) ${2022} by Kane, All Rights Reserved.
 */
import { StaffInfo } from "../../types/cpicxim/StaffInfo.js";
import { type TelSaler } from "@/types/cpicxim/TelSaler.js";

const STUFF_ITEM = "stuff_info";
const CALLER_ITEM = "call_info";
const USER_TYPE_ITEM = "user_type";

function loadStaffInfo(): StaffInfo
{
    let obj = null;

    try
    {
        obj = JSON.parse( window.localStorage.getItem( STUFF_ITEM ) ?? "{}" );
    }
    catch ( error )
    {
        obj = {};
    }

    /* eslint-disable */
    const stuff = new StaffInfo(
        obj._p13uid ?? "",
        obj._stuffCode ?? "",
        obj._stuffName ?? "",
        obj._department_code ?? "",
        obj._department_name ?? "",
        obj._section_office_code ?? "",
        obj._section_office_name ?? "" );
    /* eslint-enable */
    return stuff;
}

function saveStaffInfo( stuff: StaffInfo ): void
{
    const json = JSON.stringify( stuff );

    window.localStorage.setItem( STUFF_ITEM, json );
}

function cleanStaffInfo(): void
{
    window.localStorage.removeItem( STUFF_ITEM );
}

function saveUserType( userType: string ): void
{
    window.localStorage.setItem( USER_TYPE_ITEM, userType );
}

function getUserType(): string
{
    const userType = window.localStorage.getItem( USER_TYPE_ITEM ) ?? "";

    return userType;
}

function saveCallerInfo( callerInfo: TelSaler ): void
{
    const json = JSON.stringify( callerInfo );

    window.localStorage.setItem( CALLER_ITEM, json );
}

function getCallerInfo(): TelSaler
{
    const json = window.localStorage.getItem( CALLER_ITEM ) ?? "";

    let storeCallerInfo = null;

    try
    {
        storeCallerInfo = JSON.parse( json );
    }
    catch ( error )
    {
        storeCallerInfo = {};
    }

    const callInfo: TelSaler = {
        telSalerCode: storeCallerInfo.telSalerCode ?? "",
        telSalerName: storeCallerInfo.telSalerName ?? "",
        teamCode: storeCallerInfo.teamCode ?? "",
        teamName: storeCallerInfo.teamName ?? "",
        departmentCode: storeCallerInfo.departmentCode ?? "",
        departmentName: storeCallerInfo.departmentName ?? "",
    };

    return callInfo;
}

function clearCallerInfo(): void
{
    window.localStorage.removeItem( CALLER_ITEM );
}

export
{
    loadStaffInfo,
    saveStaffInfo,
    cleanStaffInfo,
    saveUserType,
    getUserType,
    saveCallerInfo,
    getCallerInfo,
    clearCallerInfo
};
