import { PlayerAddClientError, PlayerAddClientErrorPram } from "../../../common/Xyc";
import Config from "../../../Config";
import UIHelp from "../../../logic/ui/UIHelp";
import { I18n } from "../../../utils/I18nUtil";
import { PlatformType } from "../../const/TypeConst";
import GameDataCenter from "../../GameDataCenter";
import IDataModel from "../../../frameWork/model/IDataModel";

export default class SystemModel extends IDataModel {
    is_open_music: boolean = false;
    is_open_sound: boolean = false;

    constructor() {
        super('system');

        window["sendClientErr"] = (strList: string[]) => {
            GameDataCenter.system.sendClientErrMsg(strList)
        }
    }

    copyStr(str: string) {
        if (typeof str != "string") {
            return false
        }
        if (cc.sys.isBrowser) {
            var input = str;
            const el = document.createElement('textarea');
            el.value = input;
            el.setAttribute('readonly', '');
            el.style.contain = 'strict';
            el.style.position = 'absolute';
            el.style.left = '-9999px';
            el.style.fontSize = '12pt'; // Prevent zooming on iOS

            const selection = getSelection();
            let originalRange: Range = null;
            if (selection.rangeCount > 0) {
                originalRange = selection.getRangeAt(0);
            }
            document.body.appendChild(el);
            el.select();
            el.selectionStart = 0;
            el.selectionEnd = input.length;

            var success = false;
            try {
                success = document.execCommand('copy');
            } catch (err) { }

            document.body.removeChild(el);

            if (originalRange) {
                selection.removeAllRanges();
                selection.addRange(originalRange);
            }
            UIHelp.ShowI18nTips("SystemModel_showtips")
        } else if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.setClipboardData({
                data: str,
                success(res) {
                    wx.getClipboardData({
                        success(res) {
                            console.log(res.data)
                        }
                    })
                }
            })
        } else if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "copyStr", "(Ljava/lang/String;)V", str);
            UIHelp.ShowI18nTips("SystemModel_showtips")
        }
    }

    triggerGc() {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.triggerGC()
        } else if (GameDataCenter.plat.type == PlatformType.ylTiktok) {
            // tt.triggerGC()
        }
    }

    // 清除应用缓存,谨用
    clearCache() {
        UIHelp.ShowDialog({
            content: I18n.getI18nText('SystemModel_dialog'),
            cbConfirm: () => {
                if (cc.sys.platform == cc.sys.WECHAT_GAME) {
                    cc.assetManager.cacheManager.clearCache()
                    GameDataCenter.plat.instance.restart(true)
                } else if (GameDataCenter.plat.type == PlatformType.ylTiktok) {
                    GameDataCenter.plat.instance.restart(true)
                }
            }
        })
    }
    // 清除最近少用的缓存
    clearLRU() {
        cc.assetManager.cacheManager.clearLRU()
    }

    sendClientErrMsg(strList: string[]) {
        let param: PlayerAddClientErrorPram = {
            pid: Config.pid,
            uuid: GameDataCenter.user.uuid,
            error: strList
        }
        this.send(PlayerAddClientError.url, param)
    }
}