# ======================================================================
# 11회차 | 03 실습 - 기울여서 명령하기 빈칸   [같이 코딩]
# ----------------------------------------------------------------------
# 정답 : 01 미션 A - 기울여서 명령하기 완성.py
#
# 채울 것
#   [빈칸 1] tilt_direction : 앞(p > TILT), 뒤(p < -TILT)
#   [빈칸 2] tilt_direction : 오른쪽(r > TILT), 왼쪽(r < -TILT)
#   [빈칸 3] 기록한 명령들을 순서대로 실행하는 for (STEP cm 씩, 움직일 때마다 0.5초 쉬기)
#
# 빈칸 1, 2 가 비어 있으면 기울여도 명령이 들어가지 않는다. (Ctrl+C 로 끝내기)
# ======================================================================

from tello import start

tello_id = "TELLO-58C5B7"      # 내 드론 이름으로 바꾸기

PITCH_SIGN = -1
ROLL_SIGN = 1
TILT = 35
LEVEL = 8
FLOOR_TOF = 15
MAX_CMDS = 4
STEP = 60

NAMES = {"forward": "앞", "back": "뒤", "left": "왼쪽", "right": "오른쪽"}


def read_tilt():
    s = get_state()
    return s.get("pitch", 0) * PITCH_SIGN, s.get("roll", 0) * ROLL_SIGN


def tilt_direction(p, r):
    if abs(p) >= abs(r):
        pass
        # [빈칸 1]

    else:
        pass
        # [빈칸 2]

    return None


def on_floor():
    return get_state().get("tof", 999) <= FLOOR_TOF


def run():
    cmds = []
    print("드론을 들고 기울였다가 다시 평평하게. 다 넣었으면 바닥에 내려놓고 손 떼기.")
    level_since = None
    while True:
        p, r = read_tilt()
        d = tilt_direction(p, r)
        if d and len(cmds) < MAX_CMDS:
            time.sleep(0.4)
            if tilt_direction(*read_tilt()) == d:
                cmds.append(d)
                print(f"  {len(cmds)}번 명령: {NAMES[d]}")
                while max(abs(v) for v in read_tilt()) > LEVEL:
                    time.sleep(0.05)
            level_since = None
            continue
        if cmds and max(abs(p), abs(r)) < LEVEL and on_floor():
            if level_since is None:
                level_since = time.time()
                print("  3초 뒤 이륙 - 손을 떼세요")
            if time.time() - level_since >= 3:
                break
        else:
            level_since = None
        time.sleep(0.05)

    print(f"이륙! 명령 순서: {[NAMES[c] for c in cmds]}")
    takeoff()
    moves = {"forward": forward, "back": back, "left": left, "right": right}

    # [빈칸 3] for c in cmds : ...


    land()


start(tello_id, run)
