import {pingtaiutils} from "../WXSDK/pingtaiutils";
import { Global } from "../Global";
import {PlatfromType} from "../WXSDK/PlatfromType";

/*
* 本地数据缓存管理器
*/
export class LocalDataControl extends Laya.EventDispatcher {
    private static ins: LocalDataControl;

    constructor() {
        super();
        LocalDataControl.ins = this;
    }

    public static get Instance(): LocalDataControl {
        if (LocalDataControl.ins == null) {
            return new LocalDataControl();
        }
        return LocalDataControl.ins;
    }

    public init(): void {

    }

    /**
     * 添加本地缓存数据 object
     * @param key 
     * @param value 
     */
    public AddData(key: string, value: any): void 
    {
        this.AddLocalData(key, value);
    }

    /**
     * 获取本地缓存数据 object
     * @param key 
     */
    public getObj(key: string): any 
    {
        return this.GetLocalCaCheData(key);
    }

    /**
     * 添加本地缓存数据 string
     * @param key 
     * @param value 
     */
    public addstr(key: string, value:string): void {
        this.addCacheStr(key, value);
    }

    public addnum( key:string,value:number ):void
    {
        this.addCacheStr(key, value.toString());
    }

    /**
     * 获取本地缓存数据 string
     * @param key 
     */
    public getstr(key: string): string {
        return this.getCachestr(key);
    }

    /**
     * 获取本地缓存数据 number
     * @param key 
     */
    public getnum( key:string ):number
    {
        return Number(this.getCachestr(key));
    }

    /**
     * 清理指定本地缓存数据
     * @param key 
     */
    public removeCache(key: string): void {
        this.removeselectCache(key);
    }

    /**
     * 清理所有本地缓存数据
     */
    public clearAll(): void {
        this.clearAllCache();
    }

    public AddLocalData(key, value): void 
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                this.setStorageSyncWX(key, value);
            break;
            default:
                Laya.LocalStorage.setJSON(key, value);
            break;
        }
    }

    public GetLocalCaCheData(key: string): any 
    {
        var obj:any;
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                obj = this.getStorageSyncWX(key);
            break;
            default:
                obj = Laya.LocalStorage.getJSON(key);
            break;
        }
        if( !obj )
        {
            obj = {};
        }
        return obj;
    }

    public addCacheStr(key: string, value: any): void 
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                this.setStorageSyncWX(key, value);
            break;
            default:
                Laya.LocalStorage.setItem(key, value);
            break;
        }
    }

    public getCachestr(key: string): string {
        var str: string = "0";
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
            str = this.getStorageSyncWX(key);
            break;
            default:
            str = Laya.LocalStorage.getItem(key);
            break;
        }
        if (str == null || str == "") 
        {
            str = "0";
        }
        return str;
    }

    public removeselectCache(key: string): void 
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                this.removeStorageWX(key);
            break;
            default:
                Laya.LocalStorage.removeItem(key);
            break;
        }
    }
    
    public clearAllCache():void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                this.clearStorageWX();
            break;
            default:
                Laya.LocalStorage.clear();
            break;
        }
    }

    /**
     * 本地存储
     * @param key 
     * @param data 
     */
    public setStorageSyncWX(key:string, data:any) 
    {
        var wx = Laya.Browser.window.wx;
        if( wx )
        {
            wx.setStorageSync(key, data);
        }
    }
    
    /**
     * 获取本地存储
     * @param key 
     */
    public getStorageSyncWX(key:string):any 
    {
        var wx = Laya.Browser.window.wx;
        if( wx )
        {
            return wx.getStorageSync(key);
        }
        return null;
    }

    /**
     * 从本地缓存中移除指定 key
     * @param key 
     */
    public removeStorageWX(key:string):void
    {
        if( Laya.Browser.window.wx )
        {
            Laya.Browser.window.wx.removeStorage(key);
        }
    }

    /**
     * 清理存储数据
     */
    public clearStorageWX():void
    {
        if( Laya.Browser.window.wx )
        {
            Laya.Browser.window.wx.clearStorageSync();
        }
    }
}