#!/usr/bin/env python
#version    :       2024.03.15
#language   :       en
#camera     :       CR5205

import os
from maix import display
from maix import image
from maix import camera
import time
import os
import sys
sys.path.append('/root/')
from CocoPi import BUTTON

key_A = BUTTON(14)
key_B = BUTTON(8)
key_C = BUTTON(13)
key_D = BUTTON(7)
camera.camera.config(size=(320,240))
image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")

###需要查询的文件夹目录及文件格式后缀###
def getImgFileList(filePath="/root/user/img/",selectFile=".jpg"):
    import os
    FileList=[]
    aimFilePath=filePath
    aimFile=selectFile
    aimFileLenth=len(selectFile)*(-1)
    for root,dirs,files in os.walk(aimFilePath):
        for name in files:
            if str(name)[aimFileLenth:]==aimFile:
                FileList.append(name)
    return FileList
pngImgShow=getImgFileList()
picNum=len(pngImgShow)
picSelect=0
deleteSig=False

canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
blackBlock=image.new(size=(320, 24), mode="RGB", color=(0, 0, 0))

while True:
    if picNum==0:
        canvas.clear()
        canvas.draw_string(24, 100, "Not found any jpg format picture!", 1, color=(255, 0, 0))
    else:
        if key_B.is_pressed():
            while key_B.is_pressed():
                time.sleep(0.001)
            deleteSig=True
            os.system("rm /root/user/img/"+pngImgShow[picSelect])
            pngImgShow=getImgFileList()
            picNum=len(pngImgShow)
        else:
            if key_D.is_pressed():
                while key_D.is_pressed():
                    time.sleep(0.001)
                picSelect=picSelect-1
            elif key_C.is_pressed():
                while key_C.is_pressed():
                    time.sleep(0.001)
                picSelect=picSelect+1
            else:
                picSelect=picSelect
                
            if picSelect>picNum-1:
                picSelect=0
            elif picSelect<0:
                picSelect=picNum-1
            else:
                pass
            canvas.clear()
            try:
                canvas.draw_image((image.open("/root/user/img/"+pngImgShow[picSelect])),0,0)
            except:
                canvas.draw_string(64, 100, "Can't open this picture!", 1, color=(255,0, 0))
            canvas.draw_string(60, 218, pngImgShow[picSelect], color=(255,255,255), thickness = 1)
            
    
    canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8)
    canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8)
    canvas.draw_image((image.open("/root/preset/img/trashBox_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1)
    #canvas.draw_image((image.open("/root/preset/img/blackBlock_000000_320x33.png")).rotate(0, adjust=0),0,0,alpha=0.3)
    #canvas.draw_string(8, 39+30*demo_j,demoListText[demoSelect] , color=color_white, thickness = 1)
    if picNum==0:
        canvas.draw_string(116, 3, str(picSelect), color=(255,255,255), thickness = 1)
    else:
        canvas.draw_string(116, 3, str(picSelect+1), color=(255,255,255), thickness = 1)
    canvas.draw_string(160, 3, "/   "+str(picNum), color=(255,255,255), thickness = 1)
    
    display.show(canvas)