"""
步骤：
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 = "huilima-3"


# 运动鞋详情页

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

        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\huilima\3".format(os.getcwd())
        print("run huilima-3 ")
        self.base_bg_color = (255, 255, 255)
        self.deal_pic_func_list = [
            self.deal_pic_1,
            self.deal_pic_2,
            self.deal_pic_2_2,
            self.deal_pic_2_3,
            self.deal_pic_3,
            self.deal_pic_4,
            self.deal_pic_5,
        ]
        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())
        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_other(self):
        pp_bg = PictureProcessing(r"{}\2.jpg".format(self.root))
        goods_art_no_list = list(self.data.keys())
        # 粘贴多色图，步骤
        # 创建一个白色底大画布，循环粘贴鞋子，先粘贴所有的阴影图，再粘贴透明图，记录最大边。
        # 剪裁再粘贴到背景上

        pp_jpg_1_list = []
        pp_png_1_list = []
        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_1, pp_png_1 = self.image_one_pic(return_orign=True,
                                                    goods_art_no=goods_art_no,
                                                    name="俯视",
                                                    )
            pp_jpg_1_list.append(pp_jpg_1)
            pp_png_1_list.append(pp_png_1)

        x, y = 0, 0
        pp_bg_jpg = PictureProcessing("RGB", (1600, 1600), (255, 255, 255))
        if len(pp_jpg_1_list) == 1:
            _resize_value = 1100
        else:
            _resize_value = 900

        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 = pp_bg.to_overlay_pic_advance(top_img=pp_bg_jpg, top_png_img=pp_bg_png, base="cc")
        return pp_bg

    # 斜视图展示  多色进行拼接
    def deal_pic_1(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\1.jpg".format(self.root)))
        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 pp_jpg:
            pp_bg = PictureProcessing(r"{}\2.jpg".format(self.root))
            pp_jpg = pp_jpg.resize(value=1312)
            pp_png = pp_png.resize(value=1312)
            pp_bg = pp_bg.to_overlay_pic_advance(top_img=pp_jpg, top_png_img=pp_png, base="cc")
        else:
            pp_bg = self.deal_pic_1_other()

        detailed_images.append(pp_bg)
        pp_bg_2 = PictureProcessing(r"{}\3.jpg".format(self.root))
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 60)  # 颜色名称
        text = self.get_text_value("标题")
        if not text:
            text = self.get_text_value("商品标题")
            text = text[:6] if len(text) > 6 else text
        if not text:
            text = "复古运动鞋"
        _pp = PictureProcessing().get_text_image_advanced(
            font=font_1,
            text=text,
            fill=(255, 255, 255),
            return_mode="min_image",
        )
        pp_bg_2 = pp_bg_2.paste_img(top_img=_pp, value=(0, 362), base="nc")
        detailed_images.append(pp_bg_2)

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

    # 展示基础信息
    def deal_pic_2(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\4.jpg".format(self.root)))
        pp_bg_1 = PictureProcessing(r"{}\5.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_jpg_1 = pp_jpg_1.resize(value=1312)
        pp_png_1 = pp_png_1.resize(value=1312)
        pp_bg_1 = pp_bg_1.to_overlay_pic_advance(top_img=pp_jpg_1, top_png_img=pp_png_1, base="cc")
        # pp_bg_1.show()
        detailed_images.append(pp_bg_1)

        # 介绍卖点
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 60)  # 颜色名称
        text_list = [
            {
                "text": "卖点1标题",
                "default": "轻盈缓震，健步如飞，久走不累",
                "pos": (0, 58),
            },
            {
                "text": "卖点2标题",
                "default": "透气网面，干爽舒适，告别闷热",
                "pos": (0, 283),
            },
            {
                "text": "卖点3标题",
                "default": "柔韧大底，灵活弯折，自在穿行",
                "pos": (0, 505),
            },
        ]
        pp_bg_2 = PictureProcessing(r"{}\6.jpg".format(self.root))
        for text_data in text_list:
            text = self.get_text_value(text_data["text"])
            if not text:
                text = text_data["default"]
            _pp = PictureProcessing().get_text_image_advanced(
                font=font_1,
                text=text,
                fill=(0, 0, 0),
                return_mode="min_image",
            )
            pp_bg_2 = pp_bg_2.paste_img(top_img=_pp, value=text_data["pos"], base="nc")
        detailed_images.append(pp_bg_2)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 模特图
    def deal_pic_2_2(self):
        if self.get_text_value("模特图"):
            mote_img = PictureProcessing(self.get_text_value("模特图"))
            mote_img = mote_img.resize(value=1600)
            bg_img = PictureProcessing("RGB", (mote_img.width, mote_img.height), (255,255,255))
            bg_img = bg_img.paste_img(top_img=mote_img,base="nc", value=(0, 0))
            return bg_img
        else:
            return
    # 场景图
    def deal_pic_2_3(self):
        if self.get_text_value("场景图"):
            mote_img = PictureProcessing(self.get_text_value("场景图"))
            mote_img = mote_img.resize(value=1600)
            bg_img = PictureProcessing("RGB", (mote_img.width, mote_img.height), (255,255,255))
            bg_img = bg_img.paste_img(top_img=mote_img,base="nc", value=(0, 0))
            return bg_img
        else:
            return

    # 细节展示
    def deal_pic_3(self):
        detailed_images = []
        goods_art_no_list = list(self.data.keys())
        # 展示鞋面材质  粘贴内里顶视图
        detailed_images.append(PictureProcessing(r"{}\7.jpg".format(self.root)))
        _, 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=2254)
        pp_png_1 = pp_png_1.transpose(mode="left_right")
        pp_png_1 = pp_png_1.rotate_advance(doge=-30)

        pp_bg_1 = PictureProcessing(r"{}\8.jpg".format(self.root))
        pp_bg_1 = pp_bg_1.paste_img(top_img=pp_png_1, value=(50, 50))
        detailed_images.append(pp_bg_1)
        detailed_images.append(PictureProcessing(r"{}\9.jpg".format(self.root)))

        # 展示后跟
        _, 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=2116)
        # pp_png_1 = pp_png_1.transpose(mode="left_right")
        pp_png_2 = pp_png_2.rotate_advance(doge=30)

        pp_bg_2 = PictureProcessing(r"{}\10.jpg".format(self.root))
        pp_bg_2 = pp_bg_2.paste_img(top_img=pp_png_2, value=(100, 80), base="ne")
        detailed_images.append(pp_bg_2)
        detailed_images.append(PictureProcessing(r"{}\11.jpg".format(self.root)))
        # 展示鞋头
        _, pp_png_3 = self.image_one_pic(return_orign=True,
                                         goods_art_no=goods_art_no_list[0],
                                         name="侧视",
                                         )
        pp_png_3: PictureProcessing
        pp_png_3 = pp_png_3.resize(value=2116)
        # pp_png_1 = pp_png_1.transpose(mode="left_right")
        pp_png_3 = pp_png_3.rotate_advance(doge=30)

        pp_bg_3 = PictureProcessing(r"{}\12.jpg".format(self.root))
        pp_bg_3 = pp_bg_3.paste_img(top_img=pp_png_3, value=(294, 125), base="sw")
        detailed_images.append(pp_bg_3)
        detailed_images.append(PictureProcessing(r"{}\13.jpg".format(self.root)))

        # 展示鞋垫
        _, pp_png_4 = self.image_one_pic(return_orign=True,
                                         goods_art_no=goods_art_no_list[0],
                                         name="内里",
                                         )
        pp_png_4: PictureProcessing
        pp_png_4 = pp_png_4.resize(value=2300)
        pp_png_4 = pp_png_4.transpose(mode="left_right")
        pp_png_4 = pp_png_4.rotate_advance(doge=30)

        pp_bg_4 = PictureProcessing(r"{}\14.jpg".format(self.root))
        pp_bg_4 = pp_bg_4.paste_img(top_img=pp_png_4, value=(220, 89), base="ne")
        detailed_images.append(pp_bg_4)
        detailed_images.append(PictureProcessing(r"{}\15.jpg".format(self.root)))

        # raise 1
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示产品信息和颜色图
    def deal_pic_4(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\16.jpg".format(self.root)))
        goods_art_no_list = list(self.data.keys())
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 44)  # 颜色名称

        view_data_list = ["俯视", "内里", "后跟", "鞋底"]

        for goods_art_no_dict in self.goods_no_value["货号资料"]:
            color_name = goods_art_no_dict["颜色名称"]
            goods_art_no = goods_art_no_dict["货号"]
            pp_bg_2 = PictureProcessing(r"{}\17.jpg".format(self.root))
            # ==================
            text_list = [
                {"text": "商品货号", "pos": (83, 1137)},
            ]
            for text_data in text_list:
                text = self.get_text_value(text_data["text"])
                if text:
                    _pp = PictureProcessing().get_text_image_advanced(
                        font=font_1,
                        text=text,
                        fill=(136, 136, 136),
                        return_mode="min_image",
                    )
                    pp_bg_2 = pp_bg_2.paste_img(top_img=_pp, value=text_data["pos"])

            # ===================
            for view_name in view_data_list:
                pp_jpg, pp_png = self.image_one_pic(return_orign=True,
                                                    goods_art_no=goods_art_no,
                                                    name=view_name,
                                                    )
                pp_jpg: PictureProcessing
                pp_png: PictureProcessing

                if view_name == "俯视":
                    resize_value = 723
                    value = (418, 735)
                if view_name == "内里":
                    resize_value = 512
                    value = (1243, 273)
                if view_name == "后跟":
                    pp_jpg = pp_jpg.resize(value=500)
                    pp_png = pp_png.resize(value=500)
                    if pp_jpg.height > 382:
                        pp_png = pp_png.resize(value=330, base="high")
                        pp_jpg = pp_jpg.resize(value=330, base="high")
                    pp_jpg, pp_png = self.copy_view(pp_jpg, pp_png)
                    resize_value = 469
                    value = (1243, 681)
                if view_name == "鞋底":
                    resize_value = 512
                    value = (1243, 1111)

                pp_jpg = pp_jpg.resize(value=resize_value)
                pp_png = pp_png.resize(value=resize_value)
                value = (value[0] - pp_jpg.width / 2, value[1] - pp_jpg.height / 2)
                pp_bg_2 = pp_bg_2.to_overlay_pic_advance(top_img=pp_jpg, top_png_img=pp_png, base="nw", value=value)

            detailed_images.append(pp_bg_2)

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

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

    def copy_view(self, pp_jpg: PictureProcessing, pp_png: PictureProcessing):
        pp_bg_jpg = PictureProcessing("RGB", (1600, 1600), (255, 255, 255))
        pp_bg_png = PictureProcessing("RGBA", (1600, 1600), (255, 255, 255, 0))

        pp_jpg_1_list = [pp_jpg, pp_jpg]
        pp_png_1_list = [pp_png, pp_png]

        x, y = 0, 0
        for index, pp_jpg in enumerate(pp_jpg_1_list):
            pp_bg_jpg = pp_bg_jpg.to_overlay_pic_advance(top_img=pp_jpg,
                                                         value=(x, y))
            x += pp_jpg.width - 20

        x, y = 0, 0
        for index, pp_png in enumerate(pp_png_1_list):
            pp_bg_png = pp_bg_png.paste_img(top_img=pp_png,
                                            value=(x, y))
            x += pp_jpg.width - 20

        min_bbox = pp_bg_png.getbbox()
        min_bbox = PictureProcessing().expand_bbox(min_bbox, value=50)
        pp_bg_jpg = pp_bg_jpg.crop(bbox=min_bbox)
        pp_bg_png = pp_bg_png.crop(bbox=min_bbox)
        return pp_bg_jpg, pp_bg_png
