#!/usr/bin/env python3
import os
import random
from pathlib import Path
DEFAULT_QUESTIONS_PATH = "{}/.interview".format(str(Path.home()))

class Question:
    def __init__(self, category, que, ans):
        self.category = category
        self.que = que
        self.ans = ans

    def __repr__(self):
        return "{} ===========\n {}\n".format(self.que, self.ans)

class QuestionPool:
    def __init__(self, path):
        self.path = path
        self.questions = []
        self._read_questions(path)
        self.index = 0

    def _read_questions(self, path):
        # print("path",path)
        if os.path.isdir(path):
            childPaths = os.listdir(path)
            for p in childPaths:
                self._read_questions(os.path.join(path,p))
        if os.path.isfile(path) and path.endswith('.md'):
            q = Question(None, None, None)
            with open(path, 'r') as f:
                for x in f:
                    if x.startswith('##'):
                        if q.que != None:
                            q.ans.strip('\n')
                            q.que.strip('\n')
                            self.questions.append(q)
                        q = Question(None, None, None)
                        q.category = path
                        q.que = x
                        q.ans = ""
                    else:
                        if x != "\n" and x != None and q.ans != None:
                            q.ans += x
                if q != None:
                    self.questions.append(q)
    def next_question(self):
        self.index = self.index + random.randint(1,3)
        if self.index >= len(self.questions):
            self.index = 0
        return self.questions[self.index]

class Answer:
    def __init__(self, question, score, remark):
        self.question = question
        self.score = score
        self.remark = remark

    def __repr__(self):
        return "{} \t得分：{} \n\t备注：{}\n".format(self.question.que, self.score, self.remark)

class Interview:
    def __init__(self, qp):
        self.qp = qp  # question pool
        self.ans = []
    
    def go(self):
        if len(self.qp.questions) == 0:
            print("question pool 为空，请输入正确的题库地址，或在 ~/.interview下维护题库")
            exit()
        while True:
            question = self.qp.next_question()
            print(question)
            score = self.ask_score()
            if score == -1:
                continue
            remark = input("请输入一些备注信息，按回车结束： ")
            self.ans.append(Answer(question, int(score), remark))
            if self.next_step_guide() == -1:
                print("end!!!")
                break

    def ask_score(self):
        while True:
            score = input("请输入候选人在此题上的得分，输入其他键跳过此题 (1-5)： ")
            if score.isdigit():
                score = int(score)
                if score <= 5 and score >= 1:
                    return score
            else:
                # 表示跳过此题
                return -1

    def finish(self):
        num_ans = len(self.ans)
        sum_score = sum([a.score for a in self.ans])
        avg_score = sum_score / num_ans
        print("\n\n==================详细信息====================")
        print("候选人共回答 {} 题，总得分 {}，平均得分 {}".format(num_ans, sum_score, avg_score))
        for a in self.ans:
            print(a)

    def next_step_guide(self):
        next_step = input("回车继续，或输入 exit 退出: ")
        if next_step == '':
            return 0
        elif next_step == "exit":
            self.finish()
            return -1
        else:
            self.next_step_guide()

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-d', help = 'interview question database path')
args = parser.parse_args()
            
def main():
    path = args.d if args.d is not None else DEFAULT_QUESTIONS_PATH
    print("题库路径： ", path)
    qp = QuestionPool(path)
    interview = Interview(qp)
    interview.go()

if __name__ == "__main__":
    main()
