_ENCRYPT_=0    --LUA脚本加密

--数据类型定义
VT_LW = 1    --变量地址
VT_RW = 2    --FLASH存储

page = {
    Video = 0,
}

play = {
    x = 350, --起始x坐标
    y = 115, --起始y坐标
    h = 240, --播放视频长度
    w = 360, --播放视频宽度
    color    = 0x067F, --选中播放歌曲名称颜色
    defColor = 0x8CF1, --未选中播放
}

video = {
    NameTb    = {}, --路径
    allCnt    = 0, --视频总个数
    curPage   = 1, --页码
    allPage   = 1, --总页数
    playIndex = 0, --当前播放的音频索引
}

-- 获取扩展名 
-- 自定义函数（非回调函数，可以修改）
-- @str 需要获取后缀名的字符串
function get_extension(str)

    local file_type = ''
    local token_s, token_e = string.find(str, "%.", 1)

    if token_s ~= nil
    then
        file_type = string.sub(str, (token_s + 1), string.len(str)) 
    end

    return file_type
end

--刷新视频名称列表
function updatVideoPlayName(index)

    if index > 0
    then
        local line = index % 5
    
        if line == 0
        then
            line = 5
        end
        
        for i = 1, 5
        do
            if i == line
            then
                wgt_set_fcolor(page.Video, i, play.color)
            else
                wgt_set_fcolor(page.Video, i, play.defColor)
            end
        end
    else
        for i = 1, 5
        do
            wgt_set_fcolor(page.Video, i , play.defColor) 
        end
    end
    
end

--刷新播放歌曲名称的颜色
function updatVideoList(page)

    if video.allCnt > 0 
    then
        local index = (page - 1) * 5 
        
        for i = 1, 5 
        do
            if (index + i) <= video.allCnt 
            then
                local name = string.match(video.NameTb[index + i], ".+/(.+)")
                set_string(VT_LW, 0x1100 + (i - 1)*(0x40), name)
                set_uint16(VT_LW, 0x1500 + (i - 1)*1,   1)
            else
                set_string(VT_LW, 0x1100 + (i - 1)*(0x40), '')
                set_uint16(VT_LW, 0x1500 + (i - 1)*1,   0)
            end
        end

        if video.playIndex > 0
        then
            local cur_play_page = math.modf(video.playIndex / 5)
            if video.playIndex % 5 > 0
            then
                cur_play_page = cur_play_page + 1
            end

            if cur_play_page == page
            then
                updatVideoPlayName(video.playIndex)
            else
                updatVideoPlayName(0)
            end
        end
    end
end


--@path    :文件路径
--@filename:文件名称
--@type    :0 文件夹，1 文件
--@fsize   :文件大小
function on_list_dir(path, filename, type, fsize)

    if type == 1
    then
        local cur_file_type = get_extension(filename)
        print('filename = '..filename)
        if cur_file_type == 'mp4'
        then
            video.allCnt = video.allCnt + 1
            video.NameTb[video.allCnt] = path..'/'..filename
            print(' video.allCnt = '..video.allCnt..' /  path = '.. video.NameTb[video.allCnt])
        end 
    end

end

function on_run(screen)
    --updatMisucPlayState()
end

function on_video_notify(msg, v1, v2)

    local SysSndState     = msg   --获取播放状态
    local SysSndPlayTime  = v1    --获取当前播放的时间
    local SysSndTotalTime = v2    --获取当前播放的总时间


    if SysSndState == 0x01
    then
        set_uint16(VT_LW, 0x1006, (v1*100)//v2)
        set_string(VT_LW, 0x1240, string.format('%02d', (SysSndPlayTime // 60))..':'..(string.format('%02d', (SysSndPlayTime %60))))   --显示播放的当前的时间"xx:xx"
        set_string(VT_LW, 0x1250, string.format('%02d', (SysSndTotalTime // 60))..':'..(string.format('%02d', (SysSndTotalTime %60)))) --显示播放的总时间"xx:xx"
    
    elseif SysSndState == 0x00
    then

        video.playIndex = video.playIndex + 1 --下一首索引+1
        if video.playIndex > video.allCnt    --大于歌曲总数,播放第1首
        then
            video.playIndex = 1
        end

        local cur_play_page = math.modf(video.playIndex / 5)
        if video.playIndex % 5 > 0
        then
            cur_play_page = cur_play_page + 1
        end

        local cur_page = get_uint16(VT_LW, 0x1003)
        if cur_play_page == cur_page
        then
            updatVideoPlayName(video.playIndex)
        else
            updatVideoPlayName(0)
        end

        stop_video()
        play_video(video.NameTb[video.playIndex], play.x, play.y, play.w, play.h)

    end
    redraw()
end

function on_update(slave,vtype,addr)

    if VT_LW == vtype
    then
        if addr == 0x1000 --翻页
        then
            local val      = get_uint16(VT_LW, 0x1000)
            local cur_page = get_uint16(VT_LW, 0x1003)
            local all_page = get_uint16(VT_LW, 0x1004)

            if val == 0    --上一页
            then
                cur_page = cur_page - 1
                if cur_page <= 1
                then
                    cur_page = 1
                end
                set_uint16(VT_LW, 0x1003, cur_page)
                updatVideoList(cur_page) 

            elseif val == 1    --下一页
            then
                cur_page = cur_page + 1
                if cur_page > all_page
                then
                    cur_page = all_page
                end
                set_uint16(VT_LW, 0x1003, cur_page)
                updatVideoList(cur_page) 
            end

        elseif addr == 0x1505    --点击列表播放
        then
            local val = get_uint16(VT_LW, 0x1505)
            local cur_page = get_uint16(VT_LW, 0x1003)
            local temp = video.playIndex
            set_uint16(VT_LW, 0x1002, 1) --设置播放按钮

            video.playIndex = (cur_page - 1) * 5 + val
            if video.playIndex <= video.allCnt
            then
                if string.len(video.NameTb[video.playIndex]) > 0
                then
                    print('.... video.playIndex = '..video.playIndex)
                    updatVideoPlayName(video.playIndex)
                    if video.playIndex <= video.allCnt
                    then
                        stop_video()
                        play_video(video.NameTb[video.playIndex], play.x, play.y, play.w, play.h)
                    end
                end
            else
                video.playIndex = temp
            end

        elseif addr == 0x1001 --上下首翻页
        then
            local val = get_uint16(VT_LW, 0x1001)
            set_uint16(VT_LW, 0x1002, 1) --设置播放按钮

            if val == 0    --上一首
            then
                video.playIndex = video.playIndex - 1
                if video.playIndex < 1
                then
                    video.playIndex = video.allCnt
                end

            elseif val == 1    --下一首
            then
                video.playIndex = video.playIndex + 1
                if video.playIndex > video.allCnt
                then
                    video.playIndex = 1
                end
            end

            --刷新当前页
            local cur_play_page = math.modf(video.playIndex / 5)
            if video.playIndex % 5 > 0
            then
                cur_play_page = cur_play_page + 1
            end
            set_uint16(VT_LW, 0x1003, cur_play_page)

            stop_video()
            play_video(video.NameTb[video.playIndex], play.x, play.y, play.w, play.h)

            updatVideoList(cur_play_page)
            updatVideoPlayName(video.playIndex)

        elseif addr == 0x1002  --播放按钮
        then
            local val = get_uint16(VT_LW, 0x1002)
            if val == 0
            then
                pause_video()

            elseif val == 1
            then
                resume_video()
            end
        end
    end
end

-- 系统回调函数，插入SD卡自动回调
function on_sd_inserted(dir)

    sd_dir = dir
    list_dir(sd_dir)

    if video.allCnt > 0
    then

        video.allPage = math.modf(video.allCnt / 5)

        if video.allCnt % 5 > 0
        then
            video.allPage = video.allPage + 1
        end

        set_uint16(VT_LW, 0x1003, 1)
        set_uint16(VT_LW, 0x1004, video.allPage)
        set_uint16(VT_LW, 0x1005, 1)
        set_uint16(VT_LW, 0x1006, 0)
        set_string(VT_LW, 0x1240, '00:00') --显示播放的当前的时间"xx:xx"
        set_string(VT_LW, 0x1250, '00:00') --显示播放的总时间"xx:xx"

        updatVideoList(video.curPage)
        updatVideoPlayName(0)
    end
end

-- 系统回调函数，拔掉SD卡自动回调
function on_sd_removed()

    --复位清除相关变量
    video.NameTb   = {}
    video.allCnt   = 0
    video.curPage  = 1
    video.allPage  = 1
    video.playIndex = 0

    set_uint16(VT_LW, 0x1003, 0)
    set_uint16(VT_LW, 0x1004, 0)
    set_uint16(VT_LW, 0x1005, 0)

    for i = 1, 5 
    do
        set_string(VT_LW, 0x1100 + (i - 1)*(0x40), '')
        set_uint16(VT_LW, 0x1500 + (i - 1)*1,   0)
    end

end
