# -*- coding: utf-8 -*-
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm
from matplotlib.patches import Circle, Ellipse, FancyBboxPatch, Wedge, PathPatch
from matplotlib.path import Path
import arabic_reshaper
from bidi.algorithm import get_display
import numpy as np

FP = r"C:\Windows\Fonts\tahoma.ttf"
FPB = r"C:\Windows\Fonts\tahomabd.ttf"
fm.fontManager.addfont(FP)
TAH = fm.FontProperties(fname=FP)
TAHB = fm.FontProperties(fname=FPB) if __import__('os').path.exists(FPB) else fm.FontProperties(fname=FP, weight='bold')

NAVY="#1F3864"; BLUE="#2E5496"; TEAL="#2E8B8B"; GREEN="#3E9B4F"; LEAF="#4CAF50"; SOIL1="#5B4636"; SOIL2="#7A5C44"; GOLD="#C97A1E"

def fa(s): return get_display(arabic_reshaper.reshape(str(s)))

def draw_emblem(ax, cx, cy, R):
    # outer ring
    ax.add_patch(Circle((cx,cy), R, facecolor="white", edgecolor=TEAL, lw=R*22))
    ax.add_patch(Circle((cx,cy), R*0.93, facecolor="white", edgecolor=NAVY, lw=R*6))
    inner = Circle((cx,cy), R*0.93, facecolor="white", edgecolor="none")
    ax.add_patch(inner)
    # soil layers (clipped to inner circle, bottom part)
    clip = Circle((cx,cy), R*0.93, transform=ax.transData)
    layers = [(-0.93,-0.45,SOIL1),(-0.45,-0.18,SOIL2),(-0.18,0.02,"#8C6B4F")]
    for y0,y1,col in layers:
        rect = plt.Rectangle((cx-R, cy+R*y0), 2*R, R*(y1-y0), facecolor=col, edgecolor="none")
        rect.set_clip_path(clip); ax.add_patch(rect)
    # sprout stem
    stem_x=[cx, cx]; stem_y=[cy-R*0.18, cy+R*0.62]
    ax.plot(stem_x, stem_y, color=GREEN, lw=R*9, solid_capstyle="round", zorder=5)
    # leaves (two lower + one top)
    for ang,dx in [(38, 1),(-38,-1)]:
        e=Ellipse((cx+dx*R*0.32, cy+R*0.30), R*0.62, R*0.26, angle=ang, facecolor=LEAF, edgecolor=GREEN, lw=R*2, zorder=6)
        ax.add_patch(e)
    e=Ellipse((cx, cy+R*0.72), R*0.24, R*0.5, angle=0, facecolor=LEAF, edgecolor=GREEN, lw=R*2, zorder=6)
    ax.add_patch(e)
    # water drop accent (top-right) -> growth/water retention
    ax.add_patch(Circle((cx+R*0.5, cy+R*0.5), R*0.1, facecolor=BLUE, edgecolor="none", zorder=7))

# ---------- Horizontal lockup (for cover) ----------
fig = plt.figure(figsize=(9.2, 3.0), dpi=300)
ax = fig.add_axes([0,0,1,1]); ax.set_xlim(0,9.2); ax.set_ylim(0,3.0); ax.axis("off")
ax.set_aspect("equal")
draw_emblem(ax, 7.85, 1.5, 1.18)
# wordmark
ax.text(6.15, 1.86, fa("رویش"), fontproperties=TAHB, fontsize=58, color=NAVY, ha="right", va="center")
ax.text(6.15, 1.04, "ROUYESH", fontproperties=TAH, fontsize=24, color=TEAL, ha="right", va="center", weight="bold")
ax.text(6.15, 0.52, fa("پیت و بسترهای کشت  |  Peat & Growing Media"), fontproperties=TAH, fontsize=13.5, color="#666666", ha="right", va="center")
# divider
ax.plot([6.45,6.45],[0.55,2.45], color=TEAL, lw=2.2)
fig.savefig("logo_full.png", transparent=True, bbox_inches="tight", pad_inches=0.1)
plt.close(fig)

# ---------- Emblem + stacked wordmark (square, for letterhead) ----------
fig = plt.figure(figsize=(3.4, 3.8), dpi=300)
ax = fig.add_axes([0,0,1,1]); ax.set_xlim(0,3.4); ax.set_ylim(0,3.8); ax.axis("off"); ax.set_aspect("equal")
draw_emblem(ax, 1.7, 2.45, 1.15)
ax.text(1.7, 0.92, fa("رویش"), fontproperties=TAHB, fontsize=40, color=NAVY, ha="center", va="center")
ax.text(1.7, 0.42, "ROUYESH", fontproperties=TAH, fontsize=16, color=TEAL, ha="center", va="center", weight="bold")
fig.savefig("logo_mark.png", transparent=True, bbox_inches="tight", pad_inches=0.08)
plt.close(fig)
print("logos done")
