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


class DetailPicGetXiaoSuShuoXie(DetailBase):
    need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]

    def __init__(self, goods_no, goods_no_value: dict, out_put_dir, ):
        super().__init__(goods_no, goods_no_value, out_put_dir)
        if __name__ == '__main__':
            self.root = r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo"
        else:
            self.root = os.getcwd()

        self.detailed_images = []
        self.run()

    def run(self):
        detailed_images = self.deal_details()
        self.create_folder(self.out_put_dir)
        detail_path = "{out_put_dir}/{goods_no}/details".format(out_put_dir=self.out_put_dir, goods_no=self.goods_no)
        self.create_folder(detail_path)
        join_path = "{out_put_dir}/{goods_no}/拼接图".format(out_put_dir=self.out_put_dir, goods_no=self.goods_no)
        self.create_folder(join_path)

        for index, pp in enumerate(detailed_images):
            pp.im.save("{}/{}（{}）.jpg".format(detail_path, self.goods_no, str(index + 11).zfill(2)), format="JPEG")

        img = self.add_pic(detailed_images)
        # img.show()
        img.save("{}/1.jpg".format(join_path), format="JPEG")

        # ------------移动其他图片---------------------
        self.move_other_pic()
        return True

    def deal_details(self):
        bg_color = (246, 246, 246)
        self.image_init(bg_color)
        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())
        return [x for x in detailed_images if x]

    def deal_pic_1(self):
        bg_color = (239, 237, 238)
        """   制作主图  """
        detailed_images = []
        # -------粘贴文字-------
        font = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
        text_list = []
        for _set in [("【品    牌】", "品牌"),
                     ("【货    号】", "款号"),
                     ("【鞋面材质】", "鞋面材质"),
                     ("【系列风格】", "系列风格"),
                     ("【内里材质】", "内里材质"),
                     ("【尺    码】", "尺码"),
                     ("【鞋底材质】", "鞋底材质"),
                     ("【品牌编码】", "品牌编码"),
                     ]:
            title, key = _set
            text = self.get_text_value(key)
            if text:
                text_list.append(
                    (title, text)
                )

        # 文字排列
        pp_bg = PictureProcessing(r"{}\resources\detail_image_xiaosushuoxie_1\bg-1.jpg".format(self.root))
        x = 101
        y = 98
        for _set in text_list:
            pp_bg = pp_bg.add_text(mode="pixel",
                                   value=(x, y),
                                   font=font,
                                   text="{}:{}".format(_set[0], _set[1]),
                                   align="left",
                                   spacing=10,
                                   fill=(17, 16, 16))
            x += 588
            if x > 700:
                x = 101
                y += 94

        # ----------主图放置
        y += 10
        goods_art_no_list = list(self.data.keys())


        pp_jpg, pp_png = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="俯视", return_orign=True)
        pp_jpg = pp_jpg.resize(value=1083)
        pp_png = pp_png.resize(value=1083)
        pp_bg = pp_bg.to_overlay_pic_advance(mode="pixel", top_img=pp_jpg, base="nc", value=(0, y), top_png_img=pp_png)

        y += pp_jpg.height
        y += 20

        # ----------配图粘贴
        if len(goods_art_no_list) > 1:
            # 大于一个颜色做颜色图展示

            pp_list_1,pp_list_2 = [],[]
            for goods_art_no in goods_art_no_list:
                pp_jpg, pp_png = self.image_one_pic(goods_art_no=goods_art_no, name="侧视", return_orign=True)
                pp_jpg = pp_jpg.resize(value=400)
                pp_png = pp_png.resize(value=400)
                pp_list_1.append(pp_jpg)
                pp_list_2.append(pp_png)

            _pp_jpg = PictureProcessing()
            _pp_png = PictureProcessing()
            _pp_jpg = _pp_jpg.horizontal_distribution(pp_list=pp_list_1, bg_width=1200, margins=(0, 0, 0, 0), line_spacing=0,
                                                  number_per_row=3)
            _pp_png = _pp_png.horizontal_distribution(pp_list=pp_list_2, bg_width=1200, margins=(0, 0, 0, 0), line_spacing=0,
                                                  number_per_row=3)

            pp_bg = pp_bg.to_overlay_pic_advance(mode="pixel", top_img=_pp_jpg, base="nc", value=(0, y),
                                                 top_png_img=_pp_png)

            y += _pp_jpg.height
            y += 30

        # ------多余的剪裁
        if y < pp_bg.height:
            # print("触发剪裁")
            pp_bg = pp_bg.crop_img(value=(0, 0, pp_bg.width, y))

        return pp_bg

    def deal_pic_2(self):
        """
        细节解析

        """
        font_1 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
        font_2 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 15)
        bg_color = (220, 220, 220)
        detailed_images = []

        goods_art_no_list = list(self.data.keys())
        # ------侧视图-------------
        pp_2 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="侧视", bg_color=bg_color)
        pp_2 = pp_2.resize(mode="pixel", base="width", value=1327)
        pp_2 = PictureProcessing("RGB", (550, 550), bg_color).paste_img(mode="pixel", top_img=pp_2,
                                                                        base="wc", value=(-90, 0))
        # 文字
        pp_2 = pp_2.add_text(mode="pixel",
                             value=(21, 24),
                             font=font_1,
                             text="优质质感鞋面",
                             align="left",
                             spacing=10,
                             fill=(28, 28, 28))
        pp_2 = pp_2.add_text(mode="pixel",
                             value=(21, 76),
                             font=font_2,
                             text="颇具层次",
                             align="left",
                             spacing=10,
                             fill=(78, 78, 78))
        pp_2 = pp_2.add_text(mode="pixel",
                             value=(21, 115),
                             font=font_2,
                             text="时尚质感出众",
                             align="left",
                             spacing=10,
                             fill=(78, 78, 78))

        # ------后跟-------------
        pp_3 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="后跟", bg_color=bg_color)
        pp_3 = pp_3.resize(mode="pixel", base="width", value=625)
        pp_3 = PictureProcessing("RGB", (550, 550), bg_color).paste_img(mode="pixel", top_img=pp_3,
                                                                        base="cc", value=(0, 0))
        # 文字
        pp_3 = pp_3.add_text(mode="pixel",
                             value=(pp_3.width - 21, pp_3.height - 35 * 3),
                             font=font_2,
                             text="颇具层次",
                             align="right",
                             spacing=10,
                             anchor="rs",
                             fill=(78, 78, 78))

        pp_3 = pp_3.add_text(mode="pixel",
                             value=(pp_3.width - 21, pp_3.height - 35 * 2),
                             font=font_2,
                             text="时尚质感出众",
                             align="right",
                             anchor="rs",
                             spacing=10,
                             fill=(78, 78, 78))

        pp_3 = pp_3.add_text(mode="pixel",
                             value=(pp_3.width - 21, pp_3.height - 35),
                             font=font_1,
                             text="设计感十足",
                             anchor="rs",
                             align="right",
                             spacing=10,
                             fill=(28, 28, 28))

        # ---------------鞋底图
        pp_4 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="鞋底", bg_color=bg_color)
        pp_4 = pp_4.resize(mode="pixel", base="width", value=1635)
        pp_4 = PictureProcessing("RGB", (1134, 625), bg_color).paste_img(mode="pixel", top_img=pp_4,
                                                                         base="wc", value=(198, 0))
        # 文字
        pp_4 = pp_4.add_text(mode="pixel",
                             value=(21, 451),
                             font=font_1,
                             text="防滑耐磨鞋底",
                             align="left",
                             spacing=10,
                             fill=(28, 28, 28))
        pp_4 = pp_4.add_text(mode="pixel",
                             value=(21, 510),
                             font=font_2,
                             text="优质材质",
                             align="left",
                             spacing=10,
                             fill=(78, 78, 78))
        pp_4 = pp_4.add_text(mode="pixel",
                             value=(21, 549),
                             font=font_2,
                             text="防滑网纹设计坚固耐磨",
                             align="left",
                             spacing=10,
                             fill=(78, 78, 78))

        new_bg = PictureProcessing("RGB", (1200, 40 + pp_2.height + 40 + pp_4.height + 40), (239, 239, 239))
        new_bg = new_bg.paste_img(mode="pixel", top_img=pp_2, base="nw", value=(33, 40))
        new_bg = new_bg.paste_img(mode="pixel", top_img=pp_3, base="nw", value=(33 + 550 + 33, 40))
        new_bg = new_bg.paste_img(mode="pixel", top_img=pp_4, base="nw", value=(33, 40 + pp_2.height + 40))
        detailed_images.append(new_bg)

        return PictureProcessing(im=self.add_pic(detailed_images))

    def deal_pic_3(self):
        # 设计理念
        bg = PictureProcessing(r"{}\resources\detail_image_xiaosushuoxie_1\1 (1).jpg".format(self.root))
        font_1 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 40)
        # 粘贴文字
        text = self.get_text_value("设计理念")
        if not text:
            text = "告别枯燥沉闷日常还原自然本身色彩\n融合工装、复古、运动元素随性出走感受自在的步伐"

        bg = bg.add_text(mode="pixel",
                             value=(164, 328),
                             font=font_1,
                             text=text,
                             align="left",
                             spacing=15,
                             fill=(39, 39, 39))


        return bg

    def deal_pic_4(self):
        """   各个颜色细节展示  """
        bg_color = (255, 255, 255)
        detailed_images = []
        goods_art_no_list = list(self.data.keys())
        font_1 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)

        for goods_art_no_dict in self.goods_no_value["货号资料"]:
            color_name = goods_art_no_dict["颜色名称"]
            goods_art_no = goods_art_no_dict["货号"]
            # ------俯视图-------------
            pp_1 = self.image_one_pic(goods_art_no=goods_art_no, name="俯视", bg_color=bg_color)
            pp_1 = pp_1.resize(mode="pixel", base="width", value=956)
            pp_1 = PictureProcessing("RGB", (1200, 40 + pp_1.height + 40), bg_color).paste_img(mode="pixel",
                                                                                               top_img=pp_1,
                                                                                               base="cc", value=(0, 0))
            # 粘贴文字与logo
            pp_1 = pp_1.add_text(mode="pixel",
                                 value=(51, 26),
                                 font=font_1,
                                 text=color_name,
                                 align="left",
                                 spacing=10,
                                 fill=(28, 28, 28))
            pp_1 = pp_1.paste_img(mode="pixel", top_img=PictureProcessing(
                r"{}\resources\detail_image_xiaosushuoxie_1\1 (4).jpg".format(self.root)), base="nw", value=(51, 72))

            # ------后跟-------------
            pp_2 = self.image_one_pic(goods_art_no=goods_art_no, name="后跟", bg_color=bg_color)
            pp_2 = pp_2.resize(mode="pixel", base="width", value=345)
            pp_2 = PictureProcessing("RGB", (537, 356), bg_color).paste_img(mode="pixel", top_img=pp_2,
                                                                            base="cc", value=(0, 0))
            # ------内里-------------
            pp_3 = self.image_one_pic(goods_art_no=goods_art_no, name="内里", bg_color=bg_color)
            pp_3 = pp_3.resize(mode="pixel", base="width", value=577)
            pp_3 = PictureProcessing("RGB", (537, 356), bg_color).paste_img(mode="pixel", top_img=pp_3,
                                                                            base="cc", value=(0, 0))
            bg_pp = PictureProcessing("RGB", (1200, 20 + pp_1.height + 20 + pp_2.height + 20), bg_color)
            # 粘贴俯视图
            bg_pp = bg_pp.paste_img(mode="pixel", top_img=pp_1, base="nw", value=(0, 0))
            # 粘贴后跟
            bg_pp = bg_pp.paste_img(mode="pixel", top_img=pp_2, base="nw", value=(51, 20 + pp_1.height + 20))
            # 粘贴内里
            bg_pp = bg_pp.paste_img(mode="pixel", top_img=pp_3, base="nw", value=(588, 20 + pp_1.height + 20))

            detailed_images.append(bg_pp)

        return PictureProcessing(im=self.add_pic(detailed_images))

    def deal_pic_5(self):
        # 其他图片
        detailed_images = []
        detailed_images.append(
            PictureProcessing(r"{}\resources\detail_image_xiaosushuoxie_1\1 (2-1).jpg".format(self.root)))
        detailed_images.append(
            PictureProcessing(r"{}\resources\detail_image_xiaosushuoxie_1\1 (3).jpg".format(self.root)))

        return PictureProcessing(im=self.add_pic(detailed_images))


if __name__ == '__main__':
    import json

    with open(
            r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\qt_test\data3.txt",
            "r", encoding="utf-8") as f:
        data = json.loads(f.read())

    for goods_no, value in data.items():
        d = DetailPicGetXiaoSuShuoXie(goods_no, value,
                                      out_put_dir=r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\output\2024-11-19\软件-详情图生成")
        raise 1
