let SCRIPT_CONF = require('./common/ScriptConf.js')

/**
 * 作者: 大柒
 * 日期:  20190307
 * 
 */

let floatyWindow = {}
floatyWindow.isShow = false
floatyWindow.showWindow = function () {
    toastLog(floatyWindow.isShow)
    if(floatyWindow.isShow){
        return
    }
    threads.start(function () {
        floatyWindow.isShow = true

        var window = floaty.rawWindow(
            `  <frame id="xx1"  h="875px" w="150px" >
            <img id="cd_1" layout_gravity="bottom|center" w="100px" h="100px" src="file://./start.png" rotation="-90" />
            <img id="cd_2" layout_gravity="bottom|center" w="100px" h="100px" src="file://./resume.png" rotation="-90" />
            <img id="cd_3" layout_gravity="bottom|center" w="100px" h="100px" src="file://./back.png" rotation="-90" />
            </frame>
`);
        var window1 = floaty.window(
            ` <frame  h="150px" w="150px" >
   <img id="jia" layout_gravity="bottom|left" w="150px" h="150px" src="file://./test.png" />
</frame>`
        );


        //cd1值 菜单的默认加载位置 b,d,e 跟随C值 这个自己摸索
        //jiaodu值 加载图标的旋转开始角度 
        //sleep0值 动画时间 0为无延迟  数值越小 加载越快 反之则慢
        //date0_sj值 按下手指不移动时等待多少毫秒关闭悬浮窗
        //move0值 手指移动的距离小于该值 则判断为 未移动
        //date1_sj 值 手指触摸时间小于该值 则判断为点击
        var cd1 = 180;
        var jiaodu = 0;
        var sleep0 = 1;
        var date0_sj = 1000;
        var move0 = 60;
        var date1_sj = 200;
        var y2 = false,
            y1 = true;
        let cd2 = cd1, cd3 = cd1, cd4 = cd1
        let windowWidth
        let windowHeight
        let window1Width
        let window1Height

        ui.post(function () {
            windowWidth = window.getWidth();
            windowHeight = window.getHeight();
            window1Width = window1.getWidth();
            window1Height = window1.getHeight();
        }, 1000);
        window.cd_1.setPivotY(cd1)
        window.cd_1.setPivotX(cd1)
        window.cd_2.setPivotY(cd2)
        window.cd_2.setPivotX(cd2)
        window.cd_3.setPivotY(cd3)
        window.cd_3.setPivotX(cd3)
        window.setTouchable(false);


        //两个悬浮窗Y值差值
        var Y_cz = 800
        //两个悬浮窗初始位置 
        window.setPosition(50, device.height - (400 + Y_cz))
        window1.setPosition(50, device.height - 400)

        function jz() {
            threads.start(function () {
                if (y1) {
                    y1 = false
                    if (y2) {
                        y2 = false
                        for (let i = 0; i < 100; i++) {
                            if (i % 2) {
                                window1.jia.setRotation(jiaodu += 1)
                            }
                            window.cd_1.setPivotY(cd1 += 4)
                            window.cd_1.setPivotX(cd1)
                            window.cd_2.setPivotY(cd2 += 3)
                            window.cd_2.setPivotX(cd2)
                            window.cd_3.setPivotY(cd3 += 2)
                            window.cd_3.setPivotX(cd3)
                            sleep(sleep0)
                        }
                    } else {
                        for (let i = 100; i > 0; i--) {
                            if (i % 2) {
                                window1.jia.setRotation(jiaodu -= 1)
                            }
                            window.cd_1.setPivotY(cd1 -= 4)
                            window.cd_1.setPivotX(cd1)
                            window.cd_2.setPivotY(cd2 -= 3)
                            window.cd_2.setPivotX(cd2)
                            window.cd_3.setPivotY(cd3 -= 2)
                            window.cd_3.setPivotX(cd3)
                            sleep(sleep0)
                        }
                        y2 = true;
                    }
                    y1 = true
                }
            })
        }



        window.cd_1.on("click", () => {
            if (y2) {
                if('myEngine' in SCRIPT_CONF){
                    if (!SCRIPT_CONF.myEngine.getEngine().isDestroyed()) {
                        toast(`任务运行中: ${SCRIPT_CONF.module}`)
                        return
                    }
                }
              
                try {
                    let obj = SCRIPT_CONF.script
                    let obj1 = SCRIPT_CONF.conf
                    let obj2 = SCRIPT_CONF.module
                    let obj3 = "const ENV_CONF = " + JSON.stringify(obj1) + ";\r\n";
                    obj3 += obj
                    SCRIPT_CONF.myEngine = engines.execScript(obj2, obj3)

                } catch (err) {
                    
                }
                jz();
                window.setTouchable(false);
            }
        })
        window.cd_2.on("click", () => {
            if (y2) {
                if('myEngine' in SCRIPT_CONF){
                    if(!SCRIPT_CONF.myEngine.getEngine().isDestroyed()){
                        SCRIPT_CONF.myEngine.getEngine().forceStop()
                        toastLog(`已停止任务：${SCRIPT_CONF.module}`)
                    }
                }
                jz();
                window.setTouchable(false);
            }
        })
        window.cd_3.on("click", () => {
            if (y2) {
                if('myEngine' in SCRIPT_CONF){
                    if(!SCRIPT_CONF.myEngine.getEngine().isDestroyed()){
                        SCRIPT_CONF.myEngine.getEngine().forceStop()
                        toastLog(`已停止任务：${SCRIPT_CONF.module}`)
                    }
                }
                threads.start(function(){
                    floatyWindow.hideWindow()
                })
               
            }
        })

    


        var fn = () => {
        }
        var id = setInterval(
            fn, 3000
        )
        //记录按键被按下时的触摸坐标
        var x = 0,
            y = 0;
        //记录按键被按下时的悬浮窗位置
        var windowX, windowY;
        //记录按键被按下的时间以便判断长按等动作
        var downTime;
        yd = false;

        window1.jia.setOnTouchListener(function (view, event) {
            switch (event.getAction()) {
                case event.ACTION_DOWN:
                    x = event.getRawX();
                    y = event.getRawY();
                    windowX = window.getX();
                    windowY = window.getY();
                    windowX1 = window1.getX();
                    windowY1 = window1.getY();
                    downTime = new Date().getTime();
                    return true;
                case event.ACTION_MOVE:
                    if (!yd) { //如果移动的距离大于h值 则判断为移动 yd为真
                        if (Math.abs(event.getRawY() - y) > move0 || Math.abs(event.getRawX() - x) > move0) {
                            yd = true
                        }
                        //如果按下的时间超过g值并且yd值为假判断为长按，退出脚本 
                        if (new Date().getTime() - downTime > date0_sj) {
                            log(new Date().getTime() - downTime);
                            exit();

                            //关闭悬浮窗
                            //window.close()
                            //window1.close()
                        }
                    } else { //移动手指时调整两个悬浮窗位置

                        windowHeight = window1Height
                        windowWidth = window1Width

                        let xAxisMovingDistance = event.getRawX() - x;
                        let yAxisMovingDistance = event.getRawY() - y;
                        if (windowX + xAxisMovingDistance < 0) {
                            if (windowY + yAxisMovingDistance < 0) {
                                window.setPosition(10,
                                    windowY + (event.getRawY() - y));
                                window1.setPosition(10,
                                    windowY + ((event.getRawY() - y) + Y_cz));
                                console.log("触发了8")
                            } else if (windowY + windowHeight + yAxisMovingDistance > device.height) {
                                window.setPosition(10,
                                    device.height - windowHeight);
                                window1.setPosition(10,
                                    device.height - windowHeight + Y_cz);
                                console.log("触发了7")

                                // window.setPosition(0, );
                            } else {
                                console.log("触发了6")
                                window.setPosition(10,
                                    windowY + yAxisMovingDistance);
                                window1.setPosition(10,
                                    windowY + yAxisMovingDistance + Y_cz);

                                // window.setPosition(0, windowY + yAxisMovingDistance);
                            }
                        } else if (windowX + xAxisMovingDistance > device.width - windowWidth) {

                            if (windowY + yAxisMovingDistance < 0) {
                                console.log("触发了5")
                                window.setPosition(device.width - windowWidth,
                                    windowY + yAxisMovingDistance);
                                window1.setPosition(device.width - windowWidth,
                                    windowY + yAxisMovingDistance + Y_cz);

                            } else if (windowY + windowHeight + yAxisMovingDistance > device.height) {
                                console.log("触发了4")
                                window.setPosition(device.width - windowWidth,
                                    device.height - window1Height);
                                window1.setPosition(device.width - windowWidth,
                                    device.height - windowHeight + Y_cz);
                                // window.setPosition(device.width - windowWidth, device.height - windowHeight);
                            } else {
                                console.log("触发了3")
                                window.setPosition(device.width - windowWidth,
                                    windowY + yAxisMovingDistance);
                                window1.setPosition(device.width - windowWidth,
                                    windowY + yAxisMovingDistance + Y_cz);
                                // window.setPosition(device.width - windowWidth, windowY + yAxisMovingDistance);
                            }
                        } else {
                            if (windowY + yAxisMovingDistance < 0) {
                                console.log("触发了1")
                                window.setPosition(windowX + xAxisMovingDistance,
                                    0);
                                window1.setPosition(windowX + xAxisMovingDistance,
                                    0 + Y_cz);
                                // window.setPosition(device.width - windowWidth, windowY + yAxisMovingDistance);
                                // window.setPosition(windowX + xAxisMovingDistance, 0);
                            } else if (windowY1 + windowHeight + yAxisMovingDistance + 50 > device.height) {
                                console.log("触发了2")

                                window.setPosition(windowX + xAxisMovingDistance,
                                    device.height - windowHeight - Y_cz);
                                window1.setPosition(windowX + xAxisMovingDistance,
                                    device.height - windowHeight - 50);
                                // window.setPosition(windowX + xAxisMovingDistance, device.height - windowHeight);
                            } else {
                                window.setPosition(windowX + xAxisMovingDistance,
                                    windowY + yAxisMovingDistance);
                                window1.setPosition(windowX + xAxisMovingDistance,
                                    windowY + yAxisMovingDistance + Y_cz);
                                // window.setPosition(windowX + xAxisMovingDistance, windowY + yAxisMovingDistance);
                            }
                        }


                    }
                    return true;
                case event.ACTION_UP:
                    //触摸时间小于 200毫秒 则判断为 点击
                    if (new Date().getTime() - downTime < date1_sj) {
                        jz();
                        //根据Y2值 设置悬浮窗是否可以触摸
                        if (y2) {
                            window.setTouchable(false);
                        } else {
                            window.setTouchable(true);
                        }
                    }
                    yd = false
                    return true;
            }
            return true;
        });

    })
}
floatyWindow.hideWindow = function () {
    floatyWindow.isShow = false
    floaty.closeAll()
}

// floatyWindow.showWindow()
module.exports = floatyWindow;