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


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

        if test:
            # 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())
        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 = []
        pp_bg = PictureProcessing(r"{}\t (1).png".format(self.root))
        # 添加分割线
        # detailed_images.append(PictureProcessing("RGB", (1200, 100), (255, 255, 255)))
        detailed_images.append(pp_bg)
        # -------粘贴文字-------
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 56)
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 26)
        text_list = []

        text_list.append({"text": self.get_text_value("标题"),
                          "font": font_1,
                          "fill": (17, 16, 16),
                          })
        text_list.append({"text": self.get_text_value("副标题"),
                          "font": font_2,
                          "fill": (17, 16, 16),
                          })
        text_image = self.add_text_list(text_list, spacing=25, base="nc")
        if text_image:
            text_image = text_image.paste_img_invert(
                top_img=PictureProcessing("RGB", (1200, text_image.height + 100), self.base_bg_color),
                base="cc",
                value=(0, 0))
            detailed_images.append(text_image)
        # ====粘贴组合图以及其他图
        goods_art_no_list = list(self.data.keys())
        # 文字排列
        _zuhe_pic_list = []
        color_name_list = ["组合", "组合2", "组合3", "组合4", "组合5"]
        for color_name in color_name_list:
            pp_1 = self.get_overlay_pic_from_dict(
                goods_art_no=goods_art_no_list[0],
                color_name=color_name,
                bg_color=self.base_bg_color,
            )
            if not pp_1:
                continue
            pp_1 = pp_1.resize(value=int(pp_bg.width / 1.45))
            pp_1 = pp_1.paste_img_invert(
                top_img=PictureProcessing("RGB", (pp_bg.width, pp_1.height + 30), self.base_bg_color),
                base="cc")
            _zuhe_pic_list.append(pp_1)
            _zuhe_pic_list.append(PictureProcessing("RGB", (pp_bg.width, 150), self.base_bg_color))

        if not _zuhe_pic_list:
            pp_1 = self.get_overlay_pic_from_dict(
                goods_art_no=goods_art_no_list[0],
                color_name="俯视",
                bg_color=self.base_bg_color,
            )
            pp_1 = pp_1.resize(value=int(pp_bg.width / 1.45))
            pp_1 = pp_1.paste_img_invert(
                top_img=PictureProcessing("RGB", (pp_bg.width, pp_1.height + 30), self.base_bg_color),
                base="cc")

            _zuhe_pic_list.append(pp_1)

        detailed_images.extend(_zuhe_pic_list)
        detailed_images.append(PictureProcessing(r"{}\t (3).png".format(self.root)))
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 商品信息
    def deal_pic_2(self):
        """
        细节解析
        """
        # 文字排列
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\t (4).png".format(self.root))
        detailed_images.append(pp_bg)
        detailed_images.append(PictureProcessing("RGB", (pp_bg.width, 120), self.base_bg_color))

        goods_art_no_list = list(self.data.keys())
        pp_bg_2 = PictureProcessing(r"{}\t (26) .png".format(self.root))

        pp_1 = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no_list[0],
            color_name="侧视",
            bg_color=self.base_bg_color,
        )
        pp_1 = pp_1.resize(value=700)
        pp_bg_2 = pp_bg_2.paste_img(top_img=pp_1,
                                    base="sc",
                                    value=(0, 90)
                                    )
        # 粘贴文字
        font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 26)
        text_key_list = ["鞋面特点", "跟高", "鞋底特点"]
        for text_key in text_key_list:
            text = self.get_text_value(text_key)
            if not text:
                continue
            if text_key == "鞋面特点":
                value = (131, 380)
            if text_key == "跟高":
                value = (935, 187)
                text = "跟高:{}".format(text)
            if text_key == "鞋底特点":
                value = (831, 40)
            text_image = pp_bg_2.get_text_image_advanced(
                value=(0, 0),
                font=font,
                text=text,
                spacing=5,
                fill=(15, 15, 15),
                return_mode="min_image",
            )
            if text_image:
                pp_bg_2 = pp_bg_2.paste_img(top_img=text_image,
                                            base="sw",
                                            value=value
                                            )

        # 剪裁多余的
        if pp_bg_2.height > pp_1.height + 90:
            pp_bg_2 = pp_bg_2.crop(bbox=(0, pp_bg_2.height - 90 - pp_1.height, pp_bg_2.width, pp_bg_2.height))

        detailed_images.append(pp_bg_2)

        font_bg = PictureProcessing(r"{}\t (6).png".format(self.root))
        # 商品信息文字
        font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 35)
        text_list = [
            ("{}".format(self.get_text_value("品牌")), (294 + 20, 49)),
            ("{}".format(self.get_text_value("临时款号")), (854 + 20, 49)),
            ("{}".format(self.get_text_value("鞋面材质")), (294 + 20, 138)),
            ("{}".format(self.get_text_value("内里材质")), (854 + 20, 138)),
            ("{}".format(self.get_text_value("鞋垫材质")), (294 + 20, 230)),
            ("{}".format(self.get_text_value("鞋底材质")), (854 + 20, 230)),
        ]

        for text_item in text_list:
            if text_item[0]:
                font_bg = font_bg.get_text_image_advanced(
                    value=text_item[1],
                    font=font,
                    text=text_item[0],
                    align="center",
                    anchor=None,
                    spacing=5,
                    fill=(30, 30, 30),
                    return_mode="image",
                    margins=(0, 0, 0, 0)
                )

        # 其他图片
        detailed_images.append(font_bg)

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

    def deal_pic_3(self):
        """   各个颜色细节展示  """
        detailed_images = []
        # 添加分割线
        detailed_images.append(PictureProcessing("RGB", (1200, 100), (255, 255, 255)))

        # 放各个颜色图
        pp_list_1 = []
        font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 25)
        goods_art_no_list = list(self.data.keys())

        for index, goods_art_no in enumerate(goods_art_no_list):
            pp_jpg = self.get_overlay_pic_from_dict(
                goods_art_no=goods_art_no,
                color_name="侧视",
                bg_color=self.base_bg_color,
            )
            if pp_jpg is None:
                continue
            pp_jpg = pp_jpg.resize(value=480)
            color_name = self.goods_no_value["货号资料"][index]["颜色名称"]
            text_bg = PictureProcessing("RGB", (480, 50), (255, 255, 255))
            text_bg = text_bg.get_text_image_advanced(
                value=(480 / 2, 10),
                font=font,
                text="{}".format(color_name),
                align="center",
                anchor="mm",
                spacing=5,
                fill=(55, 55, 55),
                return_mode="image",
                margins=(0, 0, 0, 0)
            )
            # text_bg.show()
            _bg = PictureProcessing("RGB", (440, pp_jpg.height + text_bg.height), (255, 255, 255))
            _bg = _bg.paste_img(top_img=pp_jpg)
            _bg = _bg.paste_img(top_img=text_bg, value=(0, pp_jpg.height))
            pp_list_1.append(_bg)

        rows = 2
        shoes_bg = PictureProcessing().horizontal_distribution(
            pp_list=pp_list_1,
            bg_width=1200,
            margins=(0, 0, 40, 40),
            line_spacing=60,
            number_per_row=rows,
        )
        detailed_images.append(shoes_bg)
        # 添加分割线
        detailed_images.append(PictureProcessing("RGB", (1200, 200), (255, 255, 255)))

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

    # 细节展示
    def deal_pic_4(self):
        # =============细节展示================
        detail_images = []
        top_bg = PictureProcessing(r"{}\t (8).png".format(self.root))
        detail_images.append(top_bg)

        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Heavy.ttf", 120)  # 序号标签
        font_2 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 36)  # 描述主标题
        font_3 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 26)  # 描述副标题

        # ==========3个图片描述信息=========
        goods_art_no_list = list(self.data.keys())
        color_name_list = ["俯视", "鞋底", "侧视"]
        for index, color_name in enumerate(color_name_list):
            # 添加分割线
            detail_images.append(PictureProcessing("RGB", (top_bg.width, 20), (255, 255, 255)))

            # 添加商品图片
            pp_jpg = self.get_overlay_pic_from_dict(
                goods_art_no=goods_art_no_list[0],
                color_name=color_name,
                bg_color=self.base_bg_color
            )
            if color_name == "俯视":
                pp_jpg = pp_jpg.resize(value=1565)
                pp_jpg = pp_jpg.paste_img_invert(
                    top_img=PictureProcessing("RGB", (1200, 1050), (255, 255, 255)),
                    base="ws",
                    value=(104, 0))
                text_pos_value = (50, 70)
                text_pos_base = "nw"  # 粘贴文字的位置
                text_base = "wn"  # 文字方向

            if color_name == "鞋底":
                pp_jpg = pp_jpg.resize(value=1766)
                pp_jpg = pp_jpg.transpose(mode="left_right")

                pp_jpg = pp_jpg.paste_img_invert(
                    top_img=PictureProcessing("RGB", (1200, pp_jpg.height + 150), (255, 255, 255)), base="ec",
                    value=(275, 0))
                text_pos_value = (50, 150)
                text_pos_base = "es"  # 粘贴文字的位置
                text_base = "en"

            if color_name == "侧视":
                pp_jpg = pp_jpg.resize(value=1681)
                pp_jpg = pp_jpg.transpose(mode="left_right")
                pp_jpg = pp_jpg.paste_img_invert(
                    top_img=PictureProcessing("RGB", (1200, 843), (255, 255, 255)), base="ws",
                    value=(216, 0))
                text_pos_value = (50, 150)
                text_pos_base = "ws"  # 粘贴文字的位置
                text_base = "wn"

            text_list = []
            # 序号
            text_list.append({"text": "{}".format(index + 1).zfill(2),
                              "font": font_1,
                              "fill": (215, 215, 215),
                              })
            # 主标题
            text_list.append({"text": self.get_text_value("提示{}主标题".format(index + 1)),
                              "font": font_2,
                              "fill": (55, 55, 55),
                              })
            text_list.append({"text": self.get_text_value("提示{}副标题".format(index + 1)),
                              "font": font_3,
                              "fill": (55, 55, 55),
                              })

            text_image = self.add_text_list(text_list, spacing=30, base=text_base)
            pp_jpg = pp_jpg.paste_img(
                top_img=text_image,
                base=text_pos_base,
                value=text_pos_value, )
            detail_images.append(pp_jpg)
            # 添加分割线
            detail_images.append(PictureProcessing("RGB", (top_bg.width, 60), (255, 255, 255), ))

        # 添加分割线
        detail_images.append(PictureProcessing("RGB", (top_bg.width, 100), (255, 255, 255), ))
        image = PictureProcessing(im=self.add_pic(detail_images))
        return image

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

    # 静物展示
    def deal_pic_6(self):
        bg_color = (255, 255, 255)
        detailed_images = []
        goods_art_no_list = list(self.data.keys())
        top_bg = PictureProcessing(r"{}\t (11).png".format(self.root))

        detailed_images.append(top_bg)
        pp_list_1 = []
        color_name_show_list = []
        color_name_show_list.append(["组合", "内里", "侧视", "鞋底", "俯视"])
        color_name_show_list.append(["组合2", "内里", "侧视", "鞋底", "俯视"])
        color_name_show_list.append(["组合3", "内里", "侧视", "鞋底", "俯视"])
        color_name_show_list.append(["内里", "侧视", "鞋底", "俯视"])

        color_name_text_show_dict = {
            "组合": "合并展示 / MERGE",
            "组合2": "合并展示 / MERGE",
            "组合3": "合并展示 / MERGE",
            "内里": "内里展示 / INSIDER",
            "鞋底": "鞋底展示 / SOLE",
            "侧视": "后跟展示 / HEELPIECE",
            "俯视": "45°展示 / 45°",
        }
        font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 30)
        n = 0
        pp_1_height = 100

        for index, goods_art_no in enumerate(goods_art_no_list):
            try:
                color_name = color_name_show_list[index]
            except:
                color_name = color_name_show_list[-1]

            for name in color_name:
                pp_1 = self.get_overlay_pic_from_dict(
                    goods_art_no=goods_art_no,
                    color_name=name,
                    bg_color=self.base_bg_color
                )
                if pp_1:
                    n += 1
                    if name != "后跟":
                        pp_1 = pp_1.resize(value=int(top_bg.width / 1.4))
                    else:
                        pp_1 = pp_1.resize(value=int(top_bg.width / 3))

                    if n == 1:
                        pp_1_height = pp_1.height

                    _height = pp_1.height if pp_1.height > pp_1_height else pp_1_height
                    _height = pp_1.height + 50
                    # pp_1 = pp_1.paste_img_invert(base="sc",
                    #                              top_img=PictureProcessing("RGB",
                    #                                                        (int(top_bg.width / 1.4), _height + 10,),
                    #                                                        self.base_bg_color))
                    pp_1 = pp_1.paste_img_invert(base="sc",
                                                 value=(0, 50),
                                                 top_img=PictureProcessing("RGB",
                                                                           (top_bg.width, _height + 100,),
                                                                           (255, 255, 255)))

                    pp_list_1.append(pp_1)
                    font_bg = PictureProcessing("RGB", (top_bg.width, 100), (255, 255, 255))
                    font_bg = font_bg.get_text_image_advanced(
                        value=(600, 20),
                        font=font,
                        text=color_name_text_show_dict[name],
                        align="center",
                        anchor="mm",
                        spacing=5,
                        fill=(30, 30, 30),
                        return_mode="image",
                        margins=(0, 0, 0, 0)
                    )
                    pp_list_1.append(font_bg)

        if pp_list_1:
            pp_list_1.append(PictureProcessing("RGB", (top_bg.width, 100), bg_color))
            detailed_images.extend(pp_list_1)

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

    # 添加备注图
    def deal_pic_7(self):
        image_path = r"{}\t (12).png".format(self.root)
        return PictureProcessing(image_path)


