"""
步骤：
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 DetailPicGetHLM2(DetailBase):
    need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
    root = r"{}\resources\detail_temp\huilima\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,**kwargs):
        super().__init__(goods_no, goods_no_value, out_put_dir,windows=windows)
        self.root = r"{}\resources\detail_temp\huilima\1".format(os.getcwd())
        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 = (246, 246, 246)
        """   制作主图  """
        detailed_images = []
        pp_0 = PictureProcessing(r"{}\image (1).jpg".format(self.root))
        detailed_images.append(pp_0)

        goods_art_no_list = list(self.data.keys())

        pp_1 = self.data[goods_art_no_list[0]]["pic_is_deal"]["俯视"]
        pp_image = pp_1.resize(mode="relative",
                               base="by_im",
                               base_im=PictureProcessing("RGB", (604, 418), (248, 248, 248)))

        pp_image = PictureProcessing("RGB", (604, 418), bg_color).paste_img(
            mode="pixel", top_img=pp_image, base="center"
        )

        bg_img = PictureProcessing("RGB", (1200, 918), (255, 255, 255)).paste_img(
            mode="pixel", top_img=pp_image, base="nw", value=(26, 26))

        # bg_img.show()

        #   ---------- 第二张图粘贴
        pp_2 = self.data[goods_art_no_list[0]]["pic_is_deal"]["侧视"]
        pp_image = pp_2.resize(mode="relative",
                               base="by_im",
                               base_im=PictureProcessing("RGB", (604, 418), (248, 248, 248)), )

        pp_image = PictureProcessing("RGB", (604, 418), bg_color).paste_img(
            mode="pixel", top_img=pp_image, base="center"
        )

        bg_img = bg_img.paste_img(
            mode="pixel", top_img=pp_image, base="nw", value=(26, 26 + 26 + 418))

        # -------粘贴文字-------
        font = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
        text_list = [("商品款号", self.goods_no_value["款号"], 700, 297),
                     ("商品面料", self.goods_no_value["商品面料"], 700, 380),
                     ("商品内里", self.goods_no_value["商品内里"], 700, 469),
                     ("商品鞋底", self.goods_no_value["商品鞋底"], 700, 549), ]

        for i in text_list:
            bg_img = bg_img.add_text(mode="pixel",
                                     value=(i[2], i[3]),
                                     font=font,
                                     text="{}:{}".format(i[0], i[1]),
                                     align="center",
                                     spacing=10,
                                     fill=(17, 16, 16))
        detailed_images.append(bg_img)
        return PictureProcessing(im=self.add_pic(detailed_images))

    def deal_pic_2(self):
        # 尺码表
        pp_1 = PictureProcessing(r"{}\image (2).jpg".format(self.root))
        return pp_1

    def deal_pic_3(self):
        # 细节图展示
        bg_color = (246, 246, 246)

        detailed_images = []
        goods_art_no_list = list(self.data.keys())
        view_dict = {"俯视": {"crop_img_value": (-77, 401, 1086, 752),
                            "crop_img_base": "sw",
                            "paste_img_value": (0, 0),
                            "paste_img_base": "center",
                            },
                     "侧视": {"crop_img_value": (0, 0, 1086, 752),
                            "crop_img_base": "sw",
                            "paste_img_value": (0, 0),
                            "paste_img_base": "center",
                            },
                     }
        for index, goods_art_no in enumerate(goods_art_no_list):
            if index == 0:
                name_list = ["俯视", "侧视", "鞋底", "内里"]
            else:
                name_list = ["俯视", "侧视", ]

            # 第一个颜色
            if "俯视" in name_list:
                _pp = self.image_one_pic(goods_art_no, "俯视", bg_color=bg_color)
                if _pp is None:
                    continue

                bg_img = PictureProcessing("RGBA", (1200, 848), (255, 255, 255))
                _pp = (_pp.resize(value=1900)
                       .crop_img(mode="pixel", base="sw", value=(-77, 0, 1086, 752), color_fill=bg_color)
                       .radius(circular_pos=(1, 1, 1, 1), mode="relative", value=100)
                       .paste_img_invert(mode="pixel", top_img=bg_img, base="center")
                       )
                detailed_images.append(_pp)

            if "侧视" in name_list:
                _pp = self.image_one_pic(goods_art_no, "侧视", bg_color=bg_color)
                if _pp is None:
                    continue

                bg_img = PictureProcessing("RGBA", (1200, 848), (255, 255, 255))
                _pp = (_pp.resize(value=1733)
                       .crop_img(mode="pixel", base="wc", value=(715, 0, 1086, 752), color_fill=bg_color)
                       .radius(circular_pos=(1, 1, 1, 1), mode="relative", value=100)
                       .paste_img_invert(mode="pixel", top_img=bg_img, base="center")
                       )
                detailed_images.append(_pp)

            if "鞋底" in name_list:
                _pp = self.image_one_pic(goods_art_no, "鞋底", bg_color=bg_color)
                if _pp is None:
                    continue

                bg_img = PictureProcessing("RGBA", (1200, 848), (255, 255, 255))
                _pp = (_pp.resize(value=1733)
                       .crop_img(mode="pixel", base="wc", value=(715, 0, 1086, 752), color_fill=bg_color)
                       .radius(circular_pos=(1, 1, 1, 1), mode="relative", value=100)
                       .paste_img_invert(mode="pixel", top_img=bg_img, base="center")
                       )
                detailed_images.append(_pp)

            if "内里" in name_list:
                _pp = self.image_one_pic(goods_art_no, "内里", bg_color=bg_color)
                if _pp is None:
                    continue

                bg_img = PictureProcessing("RGBA", (1200, 848), (255, 255, 255))
                _pp = (_pp.resize(value=1663)
                       .crop_img(mode="pixel", base="wc", value=(-40, 0, 1086, 752), color_fill=bg_color)
                       .radius(circular_pos=(1, 1, 1, 1), mode="relative", value=100)
                       .paste_img_invert(mode="pixel", top_img=bg_img, base="center")
                       )
                detailed_images.append(_pp)

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

    def deal_pic_4(self):
        # 制作角度展示图
        bg_color = (246, 246, 246)

        detailed_images = []

        goods_art_no_list = list(self.data.keys())
        for index, goods_art_no in enumerate(goods_art_no_list):
            if index == 0:
                name_list = ["俯视", "侧视", "后跟", "鞋底"]
            else:
                name_list = ["俯视", ]

            for _name in name_list:
                # 处理图片，需要粘贴到背景等处理
                if _name not in self.data[goods_art_no]["pic_is_deal"]:
                    continue
                _pp = self.data[goods_art_no]["pic_is_deal"][_name]
                _pp = _pp.resize(mode="pixel", base="width", value=600 if _name == "后跟" else 1058, )

                bg_pp = PictureProcessing("RGB", (1200, int(_pp.height + 50)), bg_color)
                bg_pp = bg_pp.paste_img(mode="pixel", top_img=_pp, base="cc", value=(0, 0))
                detailed_images.append(bg_pp)
                # 分割线
                _p = PictureProcessing("RGB", (1200, 50), (255, 255, 255))
                detailed_images.append(_p)

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

    def deal_pic_5(self):
        return PictureProcessing(r"{}\image (3).jpg".format(self.root))
