"""
步骤：
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-2"

# 皮鞋模板


class DetailPicGet(DetailBase):
    need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
    root = r"{}\resources\detail_temp\huilima\2".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\2".format(os.getcwd())
        print("run huilima-2 ")
        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_3_2,
            self.deal_pic_3_3,
            self.deal_pic_4,
            self.deal_pic_5,
            self.deal_pic_6,
        ]
        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())
        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 = PictureProcessing(r"{}\1.jpg".format(self.root))
        detailed_images.append(pp_bg)
        goods_art_no_list = list(self.data.keys())
        # ----粘贴俯视图
        pp_jpg = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no_list[0],
            color_name="俯视",
            bg_color=self.base_bg_color,
        )
        pp_jpg = pp_jpg.resize(value=1098)
        pp_jpg = pp_jpg.paste_img_invert(
            top_img=PictureProcessing(
                "RGB", (1600, pp_jpg.height + 100), self.base_bg_color
            ),
            base="cc",
        )
        detailed_images.append(pp_jpg)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 展示设计理念
    def deal_pic_2(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\3.jpg".format(self.root)))
        detailed_images.append(PictureProcessing(r"{}\4.jpg".format(self.root)))
        detailed_images.append(PictureProcessing(r"{}\5.jpg".format(self.root)))

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

    # 角度展示
    def deal_pic_3(self):
        detailed_images = []
        goods_art_no_list = list(self.data.keys())
        # =====添加侧视图
        pp_jpg = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no_list[0],
            color_name="侧视",
            bg_color=self.base_bg_color,
        )
        pp_jpg = pp_jpg.resize(value=1098)
        pp_jpg = pp_jpg.paste_img_invert(
            top_img=PictureProcessing(
                "RGB", (1600, pp_jpg.height + 100), self.base_bg_color
            ),
            base="cc",
        )
        detailed_images.append(pp_jpg)

        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 48)  # 颜色名称
        # =====添加鞋面描述
        pp_bg_2 = PictureProcessing(r"{}\7.jpg".format(self.root))
        text = self.get_text_value("提示1主标题")
        if not text:
            text = "匠心鞋面，质感出众，型格自现"
        _pp = PictureProcessing().get_text_image_advanced(
            font=font_1,
            text=text,
            fill=(122, 122, 122),
            return_mode="min_image",
        )
        pp_bg_2 = pp_bg_2.paste_img(top_img=_pp, value=(143, 166), base="ne")
        detailed_images.append(pp_bg_2)

        pp_jpg = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no_list[0],
            color_name="俯视",
            bg_color=self.base_bg_color,
        )
        pp_jpg = pp_jpg.transpose(mode="left_right")
        pp_jpg = pp_jpg.resize(value=2210)
        pp_jpg = pp_jpg.paste_img_invert(
            top_img=PictureProcessing(
                "RGB", (1600, pp_jpg.height + 200), self.base_bg_color
            ),
            base="en",
            value=(149, 0),
        )
        detailed_images.append(pp_jpg)
        # =====添加内里图
        pp_bg_3 = PictureProcessing(r"{}\8.jpg".format(self.root))
        text = self.get_text_value("提示2主标题")
        if not text:
            text = "透气鞋面配皮内里，干爽舒适一整天"
        _pp = PictureProcessing().get_text_image_advanced(
            font=font_1,
            text=text,
            fill=(122, 122, 122),
            return_mode="min_image",
        )
        pp_bg_3 = pp_bg_3.paste_img(top_img=_pp, value=(143, 166), base="nw")
        detailed_images.append(pp_bg_3)

        pp_jpg = self.get_overlay_pic_from_dict(
            goods_art_no=goods_art_no_list[0],
            color_name="内里",
            bg_color=self.base_bg_color,
        )
        pp_jpg = pp_jpg.resize(value=2130)
        pp_jpg = pp_jpg.paste_img_invert(
            top_img=PictureProcessing(
                "RGB", (1600, pp_jpg.height + 100), self.base_bg_color
            ),
            base="wc",
            value=(294, 0),
        )
        detailed_images.append(pp_jpg)

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

    # 模特图
    def deal_pic_3_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_3_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_4(self):
        detailed_images = []
        goods_art_no_list = list(self.data.keys())
        #  添加基础信息
        font_1 = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 46)  # 颜色名称
        text_list = [
            {
                "text": "产品名称",
                "temp_text": "商品标题",
                "default_value": "",
                "pos": (294, 531),
            },
            {"text": "产地", "temp_text": "", "default_value": "浙江温州", "pos": (1235, 531)},
            {
                "text": "鞋面材质",
                "temp_text": "商品面料",
                "default_value": "",
                "pos": (429, 623),
            },
            {
                "text": "鞋底材质",
                "temp_text": "商品鞋底",
                "default_value": "",
                "pos": (1235, 623),
            },
            {
                "text": "设计理念",
                "temp_text": "",
                "default_value": "",
                "pos": (294, 721),
            },
        ]
        pp_bg_1 = PictureProcessing(r"{}\9.jpg".format(self.root))
        for text_data in text_list:
            text = self.get_text_value(text_data["text"])
            if not text:
                if text_data["temp_text"]:
                    text = self.get_text_value(text_data["temp_text"])
                if not text:
                    text = text_data["default_value"]
                if not text:
                    continue
            text = text[:13] + "..." if len(text) > 15 else text
            _pp = PictureProcessing().get_text_image_advanced(
                font=font_1,
                text=text,
                fill=(136, 136, 136),
                return_mode="min_image",
            )
            pp_bg_1 = pp_bg_1.paste_img(top_img=_pp, value=text_data["pos"])

        detailed_images.append(pp_bg_1)
        # =======添加各个颜色====多余高度需要剪裁
        pp_bg_2 = PictureProcessing(r"{}\8.jpg".format(self.root))

        font_2 = 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=390)

            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 = pp_bg_2.get_text_image_advanced(
                font=font_2,
                text="{} / COLOR".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=1114, line_spacing=10, number_per_row=2
        )

        all_color_pp = all_color_pp.paste_img_invert(
            top_img=PictureProcessing(
                "RGB", (1600, all_color_pp.height + 100), self.base_bg_color
            ),
            base="cc",
            value=(0, 0),
        )
        detailed_images.append(all_color_pp)
        return PictureProcessing(im=self.add_pic(detailed_images))

    # 产品细节展示
    def deal_pic_5(self):
        detailed_images = []
        detailed_images.append(PictureProcessing(r"{}\12.jpg".format(self.root)))

        goods_art_no_list = list(self.data.keys())
        view_list = ["侧视", "俯视", "后跟"]

        for goods_art_no_dict in self.goods_no_value["货号资料"]:
            color_name = goods_art_no_dict["颜色名称"]
            goods_art_no = goods_art_no_dict["货号"]
            for index, view_name in enumerate(view_list):
                pp_jpg = self.get_overlay_pic_from_dict(
                    goods_art_no=goods_art_no,
                    color_name=view_name,
                    bg_color=self.base_bg_color,
                )
                if view_name == "后跟":
                    pp_jpg = pp_jpg.resize(value=600)
                else:
                    pp_jpg = pp_jpg.resize(value=1202)
                pp_jpg = pp_jpg.paste_img_invert(
                    top_img=PictureProcessing(
                        "RGB", (1600, pp_jpg.height + 100), self.base_bg_color
                    ),
                    base="cc",
                    value=(0, 0),
                )
                detailed_images.append(pp_jpg)

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

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