"""
步骤：
1、整理需要处理的款号图-输出款号图文件夹
2、整理所有相关的图片作为素材图
3、按要求进行拼接
"""

from ast import mod
import os
import settings

from PIL import ImageFont

# from module.view_control.generate_goods_no_detail_pic.detail_generate_base import DetailBase
# from module.view_control.generate_goods_no_detail_pic.pic_deal import PictureProcessing
is_test_plugins = False
try:
    is_test_plugins = settings.is_test_plugins
except:
    is_test_plugins = False

if is_test_plugins:
    from custom_plugins.plugins_mode.detail_generate_base import DetailBase
    from custom_plugins.plugins_mode.pic_deal import PictureProcessing
else:
    from plugins_mode.detail_generate_base import DetailBase
    from plugins_mode.pic_deal import PictureProcessing

plugins_name = "详情模板"
company_name_list = ["轻氧驿站"]
template_name = "qingyangyizhan-1"
# "俯视", "侧视", "后跟", "鞋底", "内里" 组合，组合2

class DetailPicGet(DetailBase):
    need_view = ["俯视", "侧视", "后跟", "鞋底", "内里","组合","组合2"]
    root = r"{}\resources\detail_temp\qingyangyizhan\1".format(os.getcwd())

    def __init__(
        self,
        goods_no,
        goods_no_value: dict,
        out_put_dir,
        windows=None,
        test=False,
        excel_data=None,
        assigned_page_list=None,
    ):
        super().__init__(
            goods_no,
            goods_no_value,
            out_put_dir,
            windows=windows,
            excel_data=excel_data,
            assigned_page_list=assigned_page_list,
        )
        self.template_name = template_name

        self.root = r"{}\resources\detail_temp\qingyangyizhan\1".format(os.getcwd())
        print("run qingyangyizhan-1 ")
        self.base_bg_color = (255, 255, 255)
        self.base_bg_color_2 = (244, 242, 243)
        self.deal_pic_func_list = [
            self.deal_pic_1,
            self.deal_pic_2,
            self.deal_pic_3,
            self.deal_pic_4,
            self.deal_pic_5,
            self.deal_pic_6,
            self.deal_pic_7,
            self.deal_pic_8,
            self.deal_pic_9,
        ]

        if test:
            # pp = self.generate_font_list_to_pic()
            # pp.im.save(r"C:\Users\gymmc\Desktop\细节图示例/字号.png")
            # for k, v in self.goods_no_value.items():
            #     print(k, v)
            self.run_test()
        else:
            self.run_all()

    def run_test(self):
        detailed_images = []
        detailed_images.append(self.deal_pic_1())
        detailed_images.append(self.deal_pic_2())
        detailed_images.append(self.deal_pic_3())
        detailed_images.append(self.deal_pic_4())
        detailed_images.append(self.deal_pic_5())
        detailed_images.append(self.deal_pic_6())
        detailed_images.append(self.deal_pic_7())
        detailed_images.append(self.deal_pic_8())
        detailed_images.append(self.deal_pic_9())

        img = self.add_pic(detailed_images)
        img.save(r"{}/{}.jpg".format(self.out_put_dir, self.goods_no, format="JPEG"))
        img.show()

    # 标题文字展示
    def deal_pic_1(self):
        detailed_images = []
        top_bg = PictureProcessing(r"{}\1.jpg".format(self.root),1600)
        detailed_images.append(top_bg)
        pp_bg = PictureProcessing(r"{}\2.jpg".format(self.root),1600)
        font_main = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 80)  # 设计理念
        mainTitle = self.get_text_value("主标题")
        if mainTitle == "":
            mainTitle = "舒适新体验"
        subTitle = self.get_text_value("副标题")
        if subTitle == "":
            subTitle = "“守护热爱骑行的你”"
        describe = self.get_text_value("描述")
        if describe == "":
            describe = "简约造型/头围可调/高颜值撞色搭配"
        main_title_text_bg = PictureProcessing("RGBA", (pp_bg.width, 200), (255, 255, 255, 0))
        main_title_text_bg = main_title_text_bg.get_text_image_advanced(
            value=(0, 0),
            font=font_main,
            text=mainTitle,
            align="center",
            spacing=5,
            fill=(0, 0, 0),
            return_mode="min_image",
        )
        pp_bg = pp_bg.paste_img(top_img=main_title_text_bg, base="nc", value=(0, 242))
        font_sub = ImageFont.truetype(
            r"resources\ttf\puhui\Regular.ttf", 30
        )  # 设计理念
        sub_title_text_bg = PictureProcessing(
            "RGBA", (pp_bg.width, 200), (255, 255, 255, 0)
        )
        sub_title_text_bg = sub_title_text_bg.get_text_image_advanced(
            value=(0, 0),
            font=font_sub,
            text=subTitle,
            align="center",
            spacing=5,
            fill=(45, 50, 56),
            return_mode="min_image",
        )
        pp_bg = pp_bg.paste_img(top_img=sub_title_text_bg, base="nc", value=(0, 337))
        # 描述
        font_sub = ImageFont.truetype(
            r"resources\ttf\puhui\Regular.ttf", 24
        )  # 设计理念
        desc_title_text_bg = PictureProcessing(
            "RGBA", (pp_bg.width, 200), (255, 255, 255, 0)
        )
        desc_title_text_bg = desc_title_text_bg.get_text_image_advanced(
            value=(0, 0),
            font=font_sub,
            text=describe,
            align="center",
            spacing=5,
            fill=(45, 50, 56),
            return_mode="min_image",
        )
        pp_bg = pp_bg.paste_img(top_img=desc_title_text_bg, base="nc", value=(0, 389))
        goods_art_no_list = list(self.data.keys())
        _, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="后跟",
        )
        # pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width)
        pp_png_1 = pp_png_1.resize(value=pp_bg.width)
        pp_png_1 = pp_png_1.rotate(doge=-8)
        pp_bg = pp_bg.paste_img(top_img=pp_png_1, base="nc", value=(0, 532))
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示鞋头和后跟
    def deal_pic_2(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\3.jpg".format(self.root), 1600)
        goods_art_no_list = list(self.data.keys())
        _, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="侧视",
        )
        # pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width)
        pp_png_1 = pp_png_1.crop(mode="min")
        pp_png_1 = pp_png_1.resize(value=210)
        pp_png_1 = pp_png_1.rotate(doge=-8)
        pp_bg = pp_bg.paste_img(top_img=pp_png_1, base="sw", value=(96, 226))
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示鞋头放大图
    def deal_pic_3(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\4.jpg".format(self.root), 1600)
        goods_art_no_list = list(self.data.keys())
        _, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="鞋底",
        )
        # pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width)
        pp_png_1 = pp_png_1.resize(value=pp_bg.width)
        pp_png_1 = pp_png_1.rotate(doge=-8)
        pp_bg.paste_img(
            mode="pixel",
            top_img=pp_png_1,
            base="nc",
            value=(0, 338),
        )
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示后跟放大图
    def deal_pic_4(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\5.jpg".format(self.root))
        goods_art_no_list = list(self.data.keys())
        _, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="侧视",
        )
        pp_png_1 = pp_png_1.resize(value=pp_bg.width/1.3)
        # pp_png_1 = pp_png_1.rotate(doge=-8)
        pp_bg.paste_img(
            mode="pixel",
            top_img=pp_png_1,
            base="nc",
            value=(0, 190),
        )
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示后跟细节卡片
    def deal_pic_5(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\6.jpg".format(self.root))
        # circle = PictureProcessing(r"{}\circle.png".format(self.root))
        # goods_art_no_list = list(self.data.keys())
        # _, pp_png_1 = self.image_one_pic(
        #     return_orign=True,
        #     goods_art_no=goods_art_no_list[0],
        #     name="组合",
        # )
        # pp_png_1 = pp_png_1.crop(mode="min")
        # pp_png_1 = pp_png_1.resize(value=pp_bg.width - 50)
        # desc_title_text_bg = PictureProcessing(
        #     "RGBA", (pp_bg.width, pp_png_1.height + 150), (255, 255, 255, 0)
        # )
        # desc_title_text_bg = desc_title_text_bg.paste_img(
        #     mode="pixel",
        #     top_img=pp_png_1,
        #     base="cc",
        #     value=(0, 0),
        # )
        # desc_title_text_bg = desc_title_text_bg.rotate(doge=115)
        # pp_bg.paste_img(
        #     mode="pixel",
        #     top_img=desc_title_text_bg,
        #     base="cs",
        #     value=(0, 90),
        # )
        # pp_bg.paste_img(
        #     mode="pixel",
        #     top_img=circle,
        #     base="en",
        #     value=(30, 380),
        # )
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # "心动"设计亮点，展示旋转的侧视图
    def deal_pic_6(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\7.jpg".format(self.root))
        pp_bg_8 = PictureProcessing(r"{}\8.jpg".format(self.root))
        goods_art_no_list = list(self.data.keys())
        goods_art = goods_art_no_list[0]
        _, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art,
            name="侧视",
        )
        pp_png_1 = pp_png_1.crop(mode="min")
        pp_png_1 = pp_png_1.resize(value=pp_bg.width-100)
        desc_title_text_bg = PictureProcessing(
            "RGBA", (pp_bg.width, pp_png_1.height + 200), (255, 255, 255, 0)
        )
        desc_title_text_bg = desc_title_text_bg.paste_img(
            mode="pixel",
            top_img=pp_png_1,
            base="cc",
            value=(0, 0),
        )
        desc_title_text_bg = desc_title_text_bg.rotate(doge=150)
        desc_title_text_bg = desc_title_text_bg.crop(mode="min")
        pp_bg.paste_img(
            mode="pixel",
            top_img=desc_title_text_bg,
            base="cs",
            value=(0, 200),
        )
        detailed_images.append(pp_bg)
        detailed_images.append(pp_bg_8)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 产品展示，帮面等等
    def deal_pic_7(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\9.jpg".format(self.root))
        goods_art_no_list = list(self.data.keys())
        pp_jpg_1, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="鞋底",
        )
        pp_png_1 = pp_png_1.resize(value=pp_bg.width)
        pp_png_1 = pp_png_1.crop(mode="min")
        pp_png_1 = pp_png_1.rotate(doge=-8)
        pp_png_1 = pp_png_1.transpose()
        pp_bg.paste_img(top_img=pp_png_1, base="nc", value=(0, 520))
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 功能展示与卖点
    def deal_pic_8(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\10.jpg".format(self.root))
        goods_art_no_list = list(self.data.keys())
        goods_art = goods_art_no_list[0]
        _, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art,
            name="内里",
        )
        pp_png_1 = pp_png_1.crop(mode="min")
        pp_png_1 = pp_png_1.resize(value=300)
        desc_title_text_bg = PictureProcessing(
            "RGB", (342, 268), (255, 255, 255, 0)
        )
        desc_title_text_bg = desc_title_text_bg.paste_img(
            mode="pixel",
            top_img=pp_png_1,
            base="cc",
            value=(0, 0),
        )
        # desc_title_text_bg = desc_title_text_bg.rotate(doge=150)
        # desc_title_text_bg = desc_title_text_bg.crop(mode="min")
        desc_title_text_bg = desc_title_text_bg.radius(value=30)
        pp_bg.paste_img(
            mode="pixel",
            top_img=desc_title_text_bg,
            base="en",
            value=(62, 746),
        )
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 添加尺码表
    def deal_pic_9(self):
        image_path = r"{}\11.jpg".format(self.root)
        return PictureProcessing(image_path)
