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

import os
from PIL import ImageFont
import sys
import settings

# 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 = "zhudi-1"


# 乐福鞋 详情页


class DetailPicGet(DetailBase):
    need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
    root = r"{}\resources\detail_temp\zhudi\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,
        output_queue=None,
        **kwargs
    ):

        super().__init__(
            goods_no,
            goods_no_value,
            out_put_dir,
            windows=windows,
            excel_data=excel_data,
            assigned_page_list=assigned_page_list,
            output_queue=output_queue,
        )

        self.template_name = template_name
        self.root = r"{}\resources\detail_temp\zhudi\1".format(os.getcwd())
        print("run zhudi-1 ")
        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,
            self.deal_pic_8,
        ]
        if test:
            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())
        img = self.add_pic(detailed_images)
        if img:
            self.create_folder(r"{}/{}".format(self.out_put_dir, template_name))
            img.save(
                r"{}/{}/{}.jpg".format(
                    self.out_put_dir, template_name, self.goods_no, format="JPEG"
                )
            )
            img.show()

    #
    def deal_pic_1(self):
        detailed_images = []
        pp_bg_1 = PictureProcessing(r"{}\1.jpg".format(self.root), 1600)
        font_main = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 160)
        font_sub = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 50)
        main_text = self.get_text_value("标题")
        sub_text = self.get_text_value("副标题")
        _pp = PictureProcessing().get_text_image_advanced(
            font=font_main,
            text=main_text,
            fill=(0, 0, 0),
            spacing=20,
            return_mode="min_image",
        )
        _pp_sub = PictureProcessing().get_text_image_advanced(
            font=font_sub,
            text=sub_text,
            fill=(0, 0, 0),
            return_mode="min_image",
        )
        pp_bg_1 = pp_bg_1.paste_img(top_img=_pp, value=(0, 232), base="cs")
        pp_bg_1 = pp_bg_1.paste_img(top_img=_pp_sub, value=(0, 446), base="cs")
        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="俯视",
        )
        if not pp_jpg:
            return
        _resize_value = pp_bg_1.width
        pp_jpg = pp_jpg.resize(value=_resize_value)
        pp_png = pp_png.resize(value=_resize_value)

        pp_bg_1 = pp_bg_1.to_overlay_pic_advance(
            top_img=pp_jpg, top_png_img=pp_png, base="cc", value=(0, -100)
        )
        detailed_images.append(pp_bg_1)
        # detailed_images.append(pp_bg_2)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示基础信息
    def deal_pic_2(self):
        detailed_images = []
        pp_bg_2 = PictureProcessing(r"{}\2.jpg".format(self.root))
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 130)
        font_desc = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 48)
        text = self.get_text_value("设计理念-标题")
        text_desc = self.get_text_value("设计理念")
        _pp = PictureProcessing().get_text_image_advanced(
            font=font_1,
            text=text,
            fill=(0, 0, 0),
            return_mode="min_image",
        )
        _pp_desc = PictureProcessing().get_text_image_advanced(
            font=font_desc,
            text=text_desc,
            fill=(0, 0, 0),
            spacing=30,
            align="center",
            return_mode="min_image",
        )
        pp_bg_2 = pp_bg_2.paste_img(top_img=_pp, value=(0, 212), base="nc")
        pp_bg_2 = pp_bg_2.paste_img(top_img=_pp_desc, value=(0, 533), base="nc")
        detailed_images.append(pp_bg_2)

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

    # 颜色展示
    def deal_pic_3(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\3.jpg".format(self.root))
        font_color = ImageFont.truetype(
            r"resources\ttf\puhui\Medium.ttf", 34
        )  # 颜色名称
        color_pic_list_1 = []
        for goods_art_no_dict in self.goods_no_value["货号资料"]:
            color_name = goods_art_no_dict["颜色名称"]
            goods_art_no = goods_art_no_dict["货号"]
            pp_jpg = self.get_overlay_pic_from_dict(
                goods_art_no=goods_art_no,
                color_name="侧视",
                bg_color=self.base_bg_color,
            )
            pp_jpg = pp_jpg.resize(value=pp_bg.width / 2)

            pp_jpg = pp_jpg.paste_img_invert(
                top_img=PictureProcessing(
                    "RGBA", (pp_jpg.width, pp_jpg.height + 70), (255, 255, 255, 0)
                ),
                base="nw",
            )
            text_image = PictureProcessing().get_text_image_advanced(
                font=font_color,
                text="{}".format(color_name),
                fill=(0, 0, 0),
                return_mode="min_image",
            )
            pp_jpg = pp_jpg.paste_img(top_img=text_image, base="sc", value=(0, 10))
            color_pic_list_1.append(pp_jpg)

        # 颜色列表进行等分展示
        all_color_pp = PictureProcessing().horizontal_distribution(
            color_pic_list_1,
            bg_width=pp_bg.width - 100,
            line_spacing=10,
            number_per_row=2,
        )
        pp_bg.paste_img(top_img=all_color_pp, base="cc", value=(0, -100))
        font_category = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 50)
        text_xiemian = self.get_text_value("鞋面")
        text_xiedi = self.get_text_value("鞋底")
        text_neili = self.get_text_value("内里")
        text_xiedian = self.get_text_value("鞋垫")
        pp_text_xiemian = PictureProcessing().get_text_image_advanced(
            font=font_category,
            text=text_xiemian,
            fill=(0, 0, 0),
            return_mode="min_image",
        )
        pp_text_xiedian = PictureProcessing().get_text_image_advanced(
            font=font_category,
            text=text_xiedian,
            fill=(0, 0, 0),
            return_mode="min_image",
        )
        pp_text_xiedi = PictureProcessing().get_text_image_advanced(
            font=font_category,
            text=text_xiedi,
            fill=(0, 0, 0),
            return_mode="min_image",
        )
        pp_text_neili = PictureProcessing().get_text_image_advanced(
            font=font_category,
            text=text_neili,
            fill=(0, 0, 0),
            return_mode="min_image",
        )
        pp_bg = pp_bg.paste_img(top_img=pp_text_xiemian, value=(416, 960), base="nw")
        pp_bg = pp_bg.paste_img(top_img=pp_text_xiedian, value=(416, 1144), base="nw")
        pp_bg = pp_bg.paste_img(top_img=pp_text_neili, value=(1157, 960), base="nw")
        pp_bg = pp_bg.paste_img(top_img=pp_text_xiedi, value=(1157, 1144), base="nw")
        detailed_images.append(pp_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示细节图
    def deal_pic_4(self):
        detailed_images = []
        bg_image = PictureProcessing(r"{}\4.jpg".format(self.root))
        text_fill = (255, 255, 255)
        font_title = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 150)
        font_sub = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 50)
        text_title = self.get_text_value("细节标题-1")
        text_sub_title = self.get_text_value("细节副标题-1")
        pp_text_title = PictureProcessing().get_text_image_advanced(
            font=font_title,
            text=text_title,
            fill=text_fill,
            return_mode="min_image",
        )
        pp_text_sub_title = PictureProcessing().get_text_image_advanced(
            font=font_sub,
            text=text_sub_title,
            fill=text_fill,
            return_mode="min_image",
        )
        bg_image.paste_img(top_img=pp_text_title, base="en", value=(108, 258))
        bg_image.paste_img(top_img=pp_text_sub_title, base="en", value=(108, 578))

        goods_art_no_list = list(self.data.keys())
        _, pp_png = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="俯视",
        )
        pp_png = pp_png.resize(value=bg_image.width * 2)
        pp_png = pp_png.transpose()
        bg_image.paste_img(top_img=pp_png, base="cs", value=(-pp_png.width / 4, -200))
        detailed_images.append(bg_image)

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

    # 场景图--产品展示
    def deal_pic_5(self):
        detailed_images = []
        bg_image = PictureProcessing(r"{}\5.jpg".format(self.root))
        text_fill = (255, 255, 255)
        font_title = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 150)
        font_sub = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 50)
        text_title = self.get_text_value("细节标题-2")
        text_sub_title = self.get_text_value("细节副标题-2")
        pp_text_title = PictureProcessing().get_text_image_advanced(
            font=font_title,
            text=text_title,
            fill=text_fill,
            return_mode="min_image",
        )
        pp_text_sub_title = PictureProcessing().get_text_image_advanced(
            font=font_sub,
            text=text_sub_title,
            fill=text_fill,
            return_mode="min_image",
        )
        bg_image.paste_img(top_img=pp_text_title, base="nw", value=(115, 369))
        bg_image.paste_img(top_img=pp_text_sub_title, base="nw", value=(115, 686))

        goods_art_no_list = list(self.data.keys())
        _, pp_png = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="俯视",
        )
        pp_png = pp_png.resize(value=bg_image.width * 2)
        pp_png = pp_png.transpose()
        bg_image.paste_img(top_img=pp_png, base="cs", value=(pp_png.width / 4, 0))
        detailed_images.append(bg_image)

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

    # 场景图--产品展示
    def deal_pic_6(self):
        detailed_images = []
        bg_image = PictureProcessing(r"{}\6.jpg".format(self.root))
        text_fill = (255, 255, 255)
        font_title = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 150)
        font_sub = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 50)
        text_title = self.get_text_value("细节标题-3")
        text_sub_title = self.get_text_value("细节副标题-3")
        pp_text_title = PictureProcessing().get_text_image_advanced(
            font=font_title,
            text=text_title,
            fill=text_fill,
            return_mode="min_image",
        )
        pp_text_sub_title = PictureProcessing().get_text_image_advanced(
            font=font_sub,
            text=text_sub_title,
            fill=text_fill,
            return_mode="min_image",
        )
        bg_image.paste_img(top_img=pp_text_title, base="en", value=(108, 158))
        bg_image.paste_img(top_img=pp_text_sub_title, base="en", value=(108, 578))

        goods_art_no_list = list(self.data.keys())
        _, pp_png = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="内里",
        )
        pp_png = pp_png.resize(value=bg_image.width * 2)
        bg_image.paste_img(top_img=pp_png, base="cs", value=(-pp_png.width / 3.5, 100))
        detailed_images.append(bg_image)

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

    # 场景图--产品展示
    def deal_pic_7(self):
        detailed_images = []
        top_image = PictureProcessing(r"{}\7.jpg".format(self.root))
        text_fill = (0, 0, 0)
        font_title = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 60)
        font_sub = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 40)
        detailed_images.append(top_image)
        goods_art_no_list = list(self.data.keys())
        for goods_info in goods_art_no_list:
            pp_jpg_1_list = []
            pp_png_1_list = []
            for goods_item in goods_art_no_list:
                pp_jpg, pp_png = self.image_one_pic(
                    return_orign=True,
                    goods_art_no=goods_item,
                    name="俯视",
                )
                pp_jpg_1_list.append(pp_jpg)
                pp_png_1_list.append(pp_png)
            x, y = 0, 0
            pp_bg_jpg = PictureProcessing("RGB", (1600, 1600), (255, 255, 255))
            _resize_value = (
                top_image.width if len(pp_jpg_1_list) == 1 else top_image.width - 300
            )

            for index, pp_jpg in enumerate(pp_jpg_1_list):
                pp_jpg = pp_jpg.resize(value=_resize_value + 100 * index)
                pp_bg_jpg = pp_bg_jpg.to_overlay_pic_advance(
                    top_img=pp_jpg, value=(x, y)
                )
                x += 180
                y += 60

            x, y = 0, 0
            pp_bg_png = PictureProcessing("RGBA", (1600, 1600), (255, 255, 255, 0))
            for index, pp_png in enumerate(pp_png_1_list):
                pp_png = pp_png.resize(value=_resize_value + 100 * index)
                pp_bg_png = pp_bg_png.paste_img(top_img=pp_png, value=(x, y))
                x += 180
                y += 60

            min_bbox = pp_bg_png.getbbox()
            min_bbox = PictureProcessing().expand_bbox(min_bbox)
            pp_bg_jpg = pp_bg_jpg.crop(bbox=min_bbox)
            pp_bg_png = pp_bg_png.crop(bbox=min_bbox)
            pp_bg_1 = PictureProcessing(
                "RGB", (top_image.width, pp_bg_jpg.height), (255, 255, 255)
            )
            pp_bg_1 = pp_bg_1.to_overlay_pic_advance(
                top_img=pp_bg_jpg, top_png_img=pp_bg_png, base="cc"
            )
            pp_text_title = PictureProcessing().get_text_image_advanced(
                font=font_title,
                text="45°侧视/SIDE",
                fill=text_fill,
                return_mode="min_image",
            )
            pp_bg_1.paste_img(top_img=pp_text_title, base="cs", value=(0, 0))
            color_pic_list_1 = []
            pp_jpg_ceshi, pp_png_ceshi = self.image_one_pic(
                return_orign=True,
                goods_art_no=goods_item,
                name="侧视",
            )
            pp_jpg_hougen, pp_png_hougen = self.image_one_pic(
                return_orign=True,
                goods_art_no=goods_item,
                name="后跟",
            )
            pp_jpg_neili, pp_png_neili = self.image_one_pic(
                return_orign=True,
                goods_art_no=goods_item,
                name="内里",
            )
            pp_jpg_xiedi, pp_png_xiedi = self.image_one_pic(
                return_orign=True,
                goods_art_no=goods_item,
                name="鞋底",
            )
            pp_png_ceshi = pp_png_ceshi.resize(value=pp_bg_jpg.width / 3)
            text_image = PictureProcessing().get_text_image_advanced(
                font=font_sub,
                text="180°侧面/SIDE",
                fill=(0, 0, 0),
                return_mode="min_image",
            )
            backImage = PictureProcessing(
                "RGB", (top_image.width / 2, 400), (255, 255, 255)
            )
            backImage.paste_img(top_img=text_image, base="cs", value=(0, 0))
            backImage.paste_img(top_img=pp_png_ceshi, base="cs", value=(0, 30))
            color_pic_list_1.append(backImage)
            pp_png_hougen = pp_png_hougen.resize(value=pp_bg_jpg.width / 4.5)
            text_image = PictureProcessing().get_text_image_advanced(
                font=font_sub,
                text="背面/BACK",
                fill=(0, 0, 0),
                return_mode="min_image",
            )
            backImage = PictureProcessing(
                "RGB", (top_image.width / 2, 400), (255, 255, 255)
            )
            backImage.paste_img(top_img=text_image, base="cs", value=(0, 0))
            backImage.paste_img(top_img=pp_png_hougen, base="cs", value=(0, 30))
            color_pic_list_1.append(backImage)
            pp_png_neili = pp_png_neili.resize(value=pp_bg_jpg.width / 3)
            text_image = PictureProcessing().get_text_image_advanced(
                font=font_sub,
                text="鞋面/VAMP",
                fill=(0, 0, 0),
                return_mode="min_image",
            )
            backImage = PictureProcessing(
                "RGB", (top_image.width / 2, 400), (255, 255, 255)
            )
            backImage.paste_img(top_img=text_image, base="cs", value=(-10, 0))
            backImage.paste_img(top_img=pp_png_neili, base="cs", value=(-10, 30))
            color_pic_list_1.append(backImage)
            pp_png_xiedi = pp_png_xiedi.resize(value=pp_bg_jpg.width / 3)
            text_image = PictureProcessing().get_text_image_advanced(
                font=font_sub,
                text="鞋底/BOTTOM",
                fill=(0, 0, 0),
                return_mode="min_image",
            )
            backImage = PictureProcessing(
                "RGB", (top_image.width / 2, 400), (255, 255, 255)
            )
            backImage.paste_img(top_img=text_image, base="cs", value=(0, 0))
            backImage.paste_img(top_img=pp_png_xiedi, base="cs", value=(0, 30))
            color_pic_list_1.append(backImage)
            all_color_pp = PictureProcessing(
                "RGB", (top_image.width, 1600), (255, 255, 255)
            ).horizontal_distribution(
                color_pic_list_1,
                bg_width=top_image.width,
                line_spacing=10,
                number_per_row=2,
            )
            detailed_images.append(pp_bg_1)
            detailed_images.append(all_color_pp)
            bottomImage = PictureProcessing(
                "RGB", (top_image.width, 200), (255, 255, 255)
            )
            detailed_images.append(bottomImage)

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

    # # 添加注意事项
    def deal_pic_8(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\10.jpg".format(self.root)))
        return PictureProcessing(im=self.add_pic(detailed_images))
