import sys
sys.path.append('/root/autodl-tmp')

from TravelContentCreator.core import contentGen
from TravelContentCreator.core import posterGen
from TravelContentCreator.core import simple_collage
import json

class poster_gen_config:
    def __init__(self, json_file):
        self.json_file = json_file

    def get_info_directory(self):
        return self.json_file['info_directory']

    def get_poster_num(self):
        return self.json_file['poster_num']

    def get_input_dir(self):
        return self.json_file['input_dir']

    def get_target_size(self):
        return self.json_file['target_size']
    
    def get_output_dir(self):
        return self.json_file['output_dir']
    
    def get_tweet_content(self):
        return self.json_file['tweet_content']

def generate_poster(poster_gen_config):
    info_directory = poster_gen_config.get_info_directory()
    poster_num = poster_gen_config.get_poster_num()
    input_dir = poster_gen_config.get_input_dir()
    target_size = poster_gen_config.get_target_size()
    output_dir = poster_gen_config.get_output_dir()
    tweet_content = poster_gen_config.get_tweet_content()
    content_gen = contentGen.ContentGenerator()
    response = content_gen.run(info_directory, poster_num, tweet_content)
    # print(response)
    img_list = simple_collage.process_directory(input_dir, target_size=target_size, output_count=poster_num, output_dir=output_dir)
    poster_gen = posterGen.PosterGenerator()
    poster_config = posterGen.PosterConfig(response)
    result_path = []
    for index, item in enumerate(poster_config.get_config()):
        text_data = {
                    "title": f"{item['main_title']}",
                    "subtitle": "",
                    "additional_texts": [
                    {"text": f"{item['texts'][0]}", "position": "bottom", "size_factor": 0.5},
                    {"text": f"{item['texts'][1]}", "position": "bottom", "size_factor": 0.5}
                            ]
        }
        img_path = img_list[index]['path']
        print(img_path)
        result_path.append(poster_gen.create_poster(img_path, text_data, f"{index}.jpg"))
    return result_path

def main():
    json_file_path = "/root/autodl-tmp/TravelContentCreator/poster_gen_config.json"
    with open(json_file_path, 'r') as f:
        json_file = json.load(f)
    poster_config = poster_gen_config(json_file)
    generate_poster(poster_config)

if __name__ == "__main__":
    main()