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

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
from PIL import Image, ImageDraw
import math


class DetailPicGetXiaoSuShuoXie5(DetailBase):
    need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
    root = r"{}\resources\detail_temp\xiaosushuoxie\5".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.root = r"{}\resources\detail_temp\xiaosushuoxie\5".format(os.getcwd())
        print("run xiaosushuoxie-5 ")
        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,
            self.deal_pic_10,
            self.deal_pic_11,
        ]

        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())
        detailed_images.append(self.deal_pic_10())
        detailed_images.append(self.deal_pic_11())

        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):
        pp_bg = PictureProcessing(r"{}\t (1).png".format(self.root))

        # -------粘贴文字-------
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 74)  # 设计理念
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 44)  # 描述主标题1
        font_3 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 44)  # 描述副标题1

        text_list = []
        text_list.append({"text": self.get_text_value("设计理念"),
                          "font": font_1,
                          "fill": (47, 44, 51),
                          "spacing": 50,
                          })
        # 主标题
        text_list.append({"text": self.get_text_value("标题"),
                          "font": font_2,
                          "fill": (124, 91, 24),
                          })
        text_list.append({"text": self.get_text_value("副标题"),
                          "font": font_3,
                          "fill": (55, 55, 55),
                          })

        text_image = self.add_text_list(text_list, spacing=15, base="en")
        if text_image:
            pp_bg = pp_bg.paste_img(
                top_img=text_image,
                base="ne",
                value=(133, 277))

        return pp_bg

    # 展示鞋头和后跟
    def deal_pic_2(self):
        pp_bg = PictureProcessing(r"{}\t (2).png".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: PictureProcessing
        pp_png_1 = pp_png_1.resize(value=1187)
        pp_png_1 = pp_png_1.rotate_advance(doge=90)
        pp_png_1 = pp_png_1.crop(bbox=(0, 0, pp_png_1.width, 473))
        pp_bg = pp_bg.paste_img(top_img=pp_png_1,
                                value=(349 - pp_png_1.width / 2, 342),
                                base="sw"
                                )

        pp_jpg_2, pp_png_2 = self.image_one_pic(return_orign=True,
                                                goods_art_no=goods_art_no_list[0],
                                                name="后跟",
                                                )
        pp_png_2: PictureProcessing
        pp_png_2 = pp_png_2.resize(value=502)
        pp_png_2 = pp_png_2.crop(mode="min")
        pp_png_2 = pp_png_2.crop(bbox=(0, 0, pp_png_2.width, 473))

        pp_bg = pp_bg.paste_img(top_img=pp_png_2,
                                value=(871 - pp_png_2.width / 2, 342),
                                base="sw"
                                )

        # 添加标题文字等
        # -------粘贴文字-------
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 42)  # 特征标题
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 30)  # 特征描述

        text_list = []
        # 鞋头特征
        text_list.append({"text": self.get_text_value("鞋头特征"),
                          "font": font_1,
                          "fill": (48, 43, 49),
                          "spacing": 0,
                          })
        # 主标题
        # 鞋头描述
        text_list.append({"text": self.get_text_value("鞋头描述"),
                          "font": font_2,
                          "fill": (164, 164, 164),
                          })

        text_image = self.add_text_list(text_list, spacing=15, base="nc")
        if text_image:
            pp_bg = pp_bg.paste_img(
                top_img=text_image,
                base="nw",
                value=(349 - text_image.width / 2, 511))

        text_list = []
        # 后跟特征
        text_list.append({"text": self.get_text_value("后跟特征"),
                          "font": font_1,
                          "fill": (48, 43, 49),
                          "spacing": 0,
                          })
        # 后跟描述
        text_list.append({"text": self.get_text_value("后跟描述"),
                          "font": font_2,
                          "fill": (164, 164, 164),
                          })

        text_image = self.add_text_list(text_list, spacing=15, base="nc")
        if text_image:
            pp_bg = pp_bg.paste_img(
                top_img=text_image,
                base="nw",
                value=(871 - text_image.width / 2, 511))

        return pp_bg

    # 展示鞋头放大图
    def deal_pic_3(self):
        pp_bg = PictureProcessing(r"{}\t (3).png".format(self.root))
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 70)  # 设计理念
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 46)  # 描述主标题1
        font_3 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 44)  # 描述主标题2

        text_list = []
        # 鞋头特征
        text_list.append({"text": self.get_text_value("设计理念2"),
                          "font": font_1,
                          "fill": (3, 3, 2),
                          "spacing": 40,
                          })
        # 主标题
        text_list.append({"text": self.get_text_value("提示2主标题"),
                          "font": font_2,
                          "fill": (124, 91, 24),
                          })
        text_list.append({"text": self.get_text_value("提示2副标题"),
                          "font": font_3,
                          "fill": (26, 25, 23),
                          })

        text_image = self.add_text_list(text_list, spacing=10, base="nw")
        if text_image:
            pp_bg = pp_bg.paste_img(
                top_img=text_image,
                base="nw",
                value=(140, 176))

        # 粘贴俯视图
        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: PictureProcessing
        pp_png_1 = pp_png_1.resize(value=1933)
        pp_png_1 = pp_png_1.crop(mode="min")
        pp_bg = pp_bg.paste_img(top_img=pp_png_1,
                                value=(202, 86),
                                base="sw"
                                )

        return pp_bg

    # 展示后跟放大图
    def deal_pic_4(self):
        pp_bg = PictureProcessing(r"{}\t (4).png".format(self.root))
        h = 176
        # ===========粘贴文字============
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 46)  # 描述主标题1
        font_3 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 44)  # 描述主标题2

        text_list = []
        # 鞋头特征
        # 主标题
        text_list.append({"text": self.get_text_value("提示3主标题"),
                          "font": font_2,
                          "fill": (124, 91, 24),
                          })
        text_list.append({"text": self.get_text_value("提示3副标题"),
                          "font": font_3,
                          "fill": (26, 25, 23),
                          })

        text_image = self.add_text_list(text_list, spacing=10, base="ne")
        if text_image:
            pp_bg = pp_bg.paste_img(
                top_img=text_image,
                base="ne",
                value=(140, h))

        # 粘贴侧视图
        goods_art_no_list = list(self.data.keys())
        pp_jpg, pp_png = self.image_one_pic(return_orign=True,
                                            goods_art_no=goods_art_no_list[0],
                                            name="侧视",
                                            )
        pp_jpg = pp_jpg.resize(value=2000)
        pp_png = pp_png.resize(value=2000)

        if text_image:
            h = h + text_image.height
        else:
            h = h
        pp_bg = pp_bg.to_overlay_pic_advance(mode="pixel", top_img=pp_jpg, base="en", value=(100, h),
                                             top_png_img=pp_png)

        # 总高
        h = h + pp_jpg.height + 50
        if h < pp_bg.height:
            # 剪裁
            pp_bg = pp_bg.crop(bbox=(0, 0, 1200, h))

        return pp_bg

    # 展示后跟细节卡片
    def deal_pic_5(self):
        pp_bg = PictureProcessing(r"{}\t (5).png".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: PictureProcessing
        pp_png_1 = pp_png_1.resize(value=1621)
        pp_png_1 = pp_png_1.rotate_advance(doge=45)
        pp_png_1 = pp_png_1.transpose()
        pp_png_1 = pp_png_1.crop(mode="min")
        shoe_bg = PictureProcessing("RGB", (448, 464), self.base_bg_color)
        shoe_bg = shoe_bg.paste_img(top_img=pp_png_1,
                                    value=(-80, 200),
                                    base="wc"
                                    )
        shoe_bg = shoe_bg.radius(circular_pos=(0, 0, 1, 0), value=30)
        pp_bg = pp_bg.paste_img(top_img=shoe_bg,
                                value=(613, 174),
                                base="nw")
        # 添加文字
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 40)  # 设计理念
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 30)  # 描述主标题1

        text_list = []
        text_list.append({"text": self.get_text_value("提示4主标题"),
                          "font": font_1,
                          "fill": (10, 7, 6),
                          })
        text_list.append({"text": self.get_text_value("提示4副标题"),
                          "font": font_2,
                          "fill": (72, 65, 59),
                          })

        text_image = self.add_text_list(text_list, spacing=38, base="nw")
        if text_image:
            pp_bg = pp_bg.paste_img(
                top_img=text_image,
                base="nw",
                value=(154, 210))

        return pp_bg

    # "心动"设计亮点，展示旋转的侧视图
    def deal_pic_6(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\t (6).png".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: PictureProcessing
        pp_png_1 = pp_png_1.resize(value=2050)
        pp_png_1 = pp_png_1.transpose()
        pp_png_1 = pp_png_1.rotate_advance(doge=20)
        pp_png_1 = pp_png_1.crop(mode="min")
        pp_bg = pp_bg.paste_img(top_img=pp_png_1,
                                value=(-200, 200),
                                base="ws"
                                )

        # ========添加文字==================
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 70)  # 设计理念
        text = self.get_text_value("设计理念3")
        pp_bg = pp_bg.get_text_image_advanced(
            value=(pp_bg.width - 127, pp_bg.height - 43),
            font=font_1,
            text=text,
            anchor="rs",
            align="right",
            spacing=5,
            fill=(109, 107, 109),
            return_mode="image",
        )
        return pp_bg

    # 产品展示，展示大图各个细节
    def deal_pic_7(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\t (7).png".format(self.root)))
        goods_art_no_list = list(self.data.keys())
        goods_art_no = goods_art_no_list[0]
        # ======展示组合图============
        pp_1 = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no,
            color_name="组合",
            bg_color=self.base_bg_color_2
        )
        if pp_1:
            pp_1 = pp_1.resize(value=1200)
            pp_1 = pp_1.paste_img_invert(
                top_img=PictureProcessing("RGB", (1200, pp_1.height + 300), self.base_bg_color_2),
                base="cc", )
            detailed_images.append(pp_1)

        # ======展示鞋面===============
        detailed_images.append(PictureProcessing(r"{}\t (9).png".format(self.root)))
        pp_2 = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no,
            color_name="俯视",
            bg_color=self.base_bg_color_2
        )
        pp_2: PictureProcessing
        pp_2 = pp_2.resize(value=1519)
        pp_2 = pp_2.paste_img_invert(top_img=PictureProcessing("RGB", (1200, pp_2.height + 50), self.base_bg_color_2),
                                     base="ws", value=(171, 0))
        detailed_images.append(pp_2)

        # =======展示后跟=========
        detailed_images.append(PictureProcessing(r"{}\t (10).png".format(self.root)))
        pp_3 = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no,
            color_name="侧视",
            bg_color=self.base_bg_color_2
        )
        pp_3: PictureProcessing
        pp_3 = pp_3.resize(value=1881)
        pp_3 = pp_3.paste_img_invert(top_img=PictureProcessing("RGB", (1200, pp_3.height + 200), self.base_bg_color_2),
                                     base="ec", value=(171, 0))
        detailed_images.append(pp_3)

        # =========展示鞋底============
        detailed_images.append(PictureProcessing(r"{}\t (11).png".format(self.root)))
        pp_jpg_4, pp_png_4 = self.image_one_pic(return_orign=True,
                                                goods_art_no=goods_art_no,
                                                name="鞋底",
                                                )
        pp_png_4: PictureProcessing
        pp_png_4 = pp_png_4.resize(value=2380)
        pp_png_4 = pp_png_4.rotate_advance(doge=45)
        pp_png_4 = pp_png_4.paste_img_invert(
            top_img=PictureProcessing("RGB", (1200, pp_png_4.height + 200), self.base_bg_color_2),
            base="ws", value=(269, 81))
        detailed_images.append(pp_png_4)

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

    # 功能展示与卖点
    def deal_pic_8(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\t (12).png".format(self.root)))
        goods_art_no_list = list(self.data.keys())
        goods_art_no = goods_art_no_list[0]
        color_name_list = ["侧视", "鞋底", "俯视"]

        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 42)  # 卖点标题1
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 32)  # 卖点标题2
        font_3 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 26)  # 卖点描述1

        # =====卖点1，展示后跟========
        for color_name in color_name_list:
            bg = PictureProcessing(r"{}\t (13).png".format(self.root))
            pp_jpg, pp_png = self.image_one_pic(return_orign=True,
                                                goods_art_no=goods_art_no,
                                                name=color_name,
                                                )
            temp_bg = PictureProcessing("RGB", (536, 424), (214, 214, 214))
            if color_name == "侧视":
                pp_png = pp_png.resize(value=990)
                temp_bg = temp_bg.paste_img(top_img=pp_png, base="es")
                shoe_paste_base = "wc"
                text_1, text_2 = self.get_text_value("卖点1标题", subsection_len=2)
                text_3 = self.get_text_value("卖点1介绍")
                text_paste_base = "ec"
                text_paste_value = (312, 0)

            if color_name == "鞋底":
                pp_png = pp_png.resize(value=1000)
                temp_bg = temp_bg.paste_img(top_img=pp_png, base="wc", value=(0, 0))
                shoe_paste_base = "ec"
                text_1, text_2 = self.get_text_value("卖点2标题", subsection_len=2)
                text_3 = self.get_text_value("卖点2介绍")
                text_paste_base = "wc"
                text_paste_value = (312, 0)
            if color_name == "俯视":
                pp_png = pp_png.resize(value=1418)
                temp_bg = temp_bg.paste_img(top_img=pp_png, base="cc")
                shoe_paste_base = "wc"
                text_1, text_2 = self.get_text_value("卖点3标题", subsection_len=2)
                text_3 = self.get_text_value("卖点3介绍")
                text_paste_base = "ec"
                text_paste_value = (312, 0)

            # 添加圆角
            temp_bg = temp_bg.radius(circular_pos=(1, 1, 1, 1), value=50)
            # 粘贴到背景上
            temp_bg = temp_bg.paste_img_invert(top_img=bg, base=shoe_paste_base, value=(99, 0))
            text_list = []
            # 卖点主标题
            text_list.append({"text": text_1,
                              "font": font_1,
                              "fill": (2, 2, 2),
                              "spacing": 0,
                              })
            text_list.append({"text": text_2,
                              "font": font_2,
                              "fill": (43, 43, 43),
                              "spacing": 10,
                              })
            # 主标题
            text_list.append({"text": text_3,
                              "font": font_3,
                              "fill": (165, 165, 165),
                              })
            text_image = self.add_text_list(text_list, spacing=15, base="nc")
            if text_image:
                temp_bg = temp_bg.paste_img(
                    top_img=text_image,
                    base=text_paste_base,
                    value=(text_paste_value[0] - text_image.width / 2, text_paste_value[1]))
            detailed_images.append(temp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

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

    # 角度展示
    def deal_pic_10(self):
        detailed_images = []
        # 角度展示标题
        detailed_images.append(PictureProcessing(r"{}\t (17).png".format(self.root)))
        # 添加颜色名称字体
        font_color = ImageFont.truetype(r"resources\ttf\puhui\Bold.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.get_overlay_pic_from_dict(
                goods_art_no=goods_art_no,
                color_name="俯视",
                bg_color=(255, 255, 255)
            )
            pp_1: PictureProcessing
            pp_1 = pp_1.resize(value=865)
            pp_1 = pp_1.paste_img_invert(top_img=PictureProcessing("RGB", (1152, pp_1.height + 100), (255, 255, 255)),
                                         base="sc"
                                         )
            # 粘贴颜色名称
            pp_1 = pp_1.get_text_image_advanced(
                value=(90, 50),
                font=font_color,
                text=color_name,
                spacing=1,
                return_mode="image",
            )
            pp_1 = pp_1.paste_img_invert(top_img=PictureProcessing("RGB", (1200, pp_1.height), (243, 243, 243)),
                                         base="cc"
                                         )

            detailed_images.append(pp_1)

            # 添加处理各个角度
            view_name_list = ["内里", "鞋底", "后跟", "侧视"]
            color_pic_list = []
            for view_name in view_name_list:
                _, pp_png = self.image_one_pic(return_orign=True,
                                               goods_art_no=goods_art_no,
                                               name=view_name,
                                               )
                temp_bg = PictureProcessing("RGB", (546, 338), (243, 243, 243))

                # if view_name == "内里":
                #     pp_png = pp_png.resize(base_by_box=(temp_bg.width * 0.9, temp_bg.height * 0.9))
                # if view_name == "鞋底":
                #     pp_png = pp_png.resize(base_by_box=(temp_bg.width * 0.9, temp_bg.height * 0.9))
                # if view_name == "后跟":
                #     pp_png = pp_png.resize(base_by_box=(temp_bg.width * 0.9, temp_bg.height * 0.9))
                # if view_name == "侧视":
                #     pp_png = pp_png.resize(base_by_box=(temp_bg.width * 0.9, temp_bg.height * 0.9))

                pp_png = pp_png.resize(base_by_box=(temp_bg.width * 0.9, temp_bg.height * 0.9))

                temp_bg = temp_bg.paste_img(top_img=pp_png, base="cc")
                temp_bg = temp_bg.radius(value=30)
                color_pic_list.append(temp_bg)

            # 颜色列表进行等分展示
            all_color_pp = PictureProcessing().horizontal_distribution(color_pic_list, bg_width=1114, line_spacing=10,
                                                                       number_per_row=2)
            all_color_pp = all_color_pp.paste_img_invert(
                top_img=PictureProcessing("RGB", (1152, all_color_pp.height + 100), (255, 255, 255)), base="cc")
            all_color_pp = all_color_pp.paste_img_invert(
                top_img=PictureProcessing("RGB", (1200, all_color_pp.height), (243, 243, 243)), base="cc")
            detailed_images.append(all_color_pp)

        # 添加底部圆弧分割线
        b_bg = PictureProcessing("RGB", (1152, 30), (255, 255, 255))
        b_bg = b_bg.radius(circular_pos=(0, 0, 1, 1), value=30)
        b_bg = b_bg.paste_img_invert(top_img=PictureProcessing("RGB", (1200, b_bg.height + 50), (243, 243, 243)),
                                     base="nc")
        detailed_images.append(b_bg)

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

    # 添加注意事项
    def deal_pic_11(self):
        image_path = r"{}\t (21).png".format(self.root)
        return PictureProcessing(image_path)
