var lbGrade;
var logoIcon;
var deviceInfoContianer;
var blackMask;

function UiModeToString(uiMode) {
    switch (uiMode) {
        case "pad":
            return "平板电脑";
        case "phone":
            return "智能手机";
        case "tv":
            return "智能电视";
        case "car":
            return "车载终端";
        case "desk":
            return "桌面电脑";
        case "watch":
            return "智能手表";
        default:
            return "未知";
    }
}

function ConnectionTypeToString(connectionType) {
    switch (connectionType) {
        case "unknown":
            return "未知";
        case "ethernet":
            return "以太网";
        case "wifi":
            return "Wi-Fi";
        case "2g":
            return "2G";
        case "3g":
            return "3G";
        case "4G":
            return "4G";
        case "none":
            return "无网络";
        default:
            return "未知";
    }
}

function showDeviceInfo() {
    if (blackMask.hasClass("hidden")) {
        blackMask.removeClass("hidden");
    }
}

function hideDeviceInfo() {
    if (!blackMask.hasClass("hidden")) {
        blackMask.addClass("hidden");
    }
}

function initDeviceInfo() {
    var deviceInfo = "<span>";
    deviceInfo += ("APP" + lbVersion.html() + "<br/>");
    deviceInfo += ("操作系统：" + api.systemType + " " + api.systemVersion + "<br/>");
    deviceInfo += ("设备型号：" + api.deviceModel + "<br/>");
    deviceInfo += ("设备名称：" + api.deviceName + "<br/>");
    deviceInfo += ("设备类型：" + UiModeToString(api.uiMode) + "<br/>");
    deviceInfo += ("运 营 商：" + api.operator + "<br/>");
    deviceInfo += ("当前网络：" + ConnectionTypeToString(api.connectionType) + "<br/>");
    deviceInfo += ("分 辨 率：" + api.winWidth + "x" + api.winHeight);
    deviceInfo += "</span>";

    deviceInfoContianer.html(deviceInfo);
}

apiready = function() {
    var ctrl = {
        init: {},
        bind: {}
    }

    ctrl.bind = function() {
        //用户点击了返回按钮
        $(".back").on("touchend", function() {
            api.closeWin();
        });
        var phoneNum = $api.trim($('#phoneCall').text());
        // 点击电话号码
        $('#phoneCall').on('touchend',function() {
          api.call({
            type: 'tel_prompt',
            number: phoneNum
          })
        })
        //给我评分
        $('#lbGrade').on("touchend", function() {
                var platform = api.systemType;
                if (platform == "ios") {
                    api.openApp({
                        iosUrl: 'https://itunes.apple.com/us/app/%E7%94%B5%E5%8A%A1%E9%80%9A/id1247755464?l=zh&ls=1&mt=8'
                    }, function(ret, err) {
                        //coding...
                    });
                }
            })
            //长按显示设备信息
        api.addEventListener({
            name: "longpress"
        }, function(ret, err) {
            showDeviceInfo();
        });

        blackMask.on("touchend", function() {
            //隐藏设备信息
            hideDeviceInfo();
        });
    }

    ctrl.init = function() {
        // 适配安卓状态栏
        CommonModel.fitInStatusBar();
        lbGrade = $("#lbGrade");
        if (api.systemType != "ios") {
            lbGrade.hide();
        }

        lbVersion = $("#lbVersion");
        api.readFile({
            path: 'widget://resource/version.txt'
        }, function(ret, err) {
            if (ret.status) {
                lbVersion.html('版本：' + ret.data);
                initDeviceInfo();
            }
        });

        deviceInfoContianer = $("#deviceInfoContianer");
        blackMask = $("#blackMask");

        logoIcon = $("#logo-icon");
        ctrl.bind();
    }

    ctrl.init();
}
