"""ESM_3 (Online Resource 3)
Article: Settlement Speed and Merchant Liquidity in National QR Systems
Journal: Digital Finance
Author: Molla Md Rezaul Karim, Rupali Bank PLC, Dhaka, Bangladesh (rezakarim2003@gmail.com)

Figures for 'Settlement Speed and Merchant Liquidity in National QR Systems' (Digital Finance).
Fig. 1: analytical framework. Fig. 2: sensitivity of the quantity channel.
Run: python figs_df.py  (requires numpy, matplotlib). Writes Fig1/Fig2 as EPS, PDF, SVG and 600-dpi PNG."""
import matplotlib; matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
import numpy as np
plt.rcParams.update({'font.family':'serif','font.serif':['DejaVu Serif'],'mathtext.fontset':'dejavuserif','pdf.fonttype':42,'ps.fonttype':42})
L=10/7
# ---------- Fig. 2 (formerly A1) ----------
fig,ax=plt.subplots(figsize=(6.0,4.0))
q=np.linspace(0.05,0.70,300)
for G,ls,mk in [(2,'-','o'),(3,'--','s'),(7,'-.','^'),(14,':','D'),(30,(0,(6,2,1,2)),'v')]:
    ax.plot(q*100,100*q*L/(G+q*L),color='k',ls=ls,lw=1.2,marker=mk,markevery=60,ms=5,label=f'G = {G} days')
ax.set_xlim(5,70); ax.set_ylim(0,35)
ax.set_xlabel('QR share of sales, q (%)'); ax.set_ylabel(r'Reduction in sustainable sales capacity, $\ell$ (%)')
ax.grid(alpha=0.3,lw=0.5); ax.spines['top'].set_visible(False); ax.spines['right'].set_visible(False)
ax.legend(title=r'Net inventory cycle ($\bar{L}$ = 1.43)',frameon=False,fontsize=9,title_fontsize=9,loc='upper left')
fig.tight_layout()
for ext in ('eps','pdf','svg'): fig.savefig(f'Fig2.{ext}')
fig.savefig('Fig2.png',dpi=600); plt.close(fig)
# ---------- Fig. 1 ----------
fig,ax=plt.subplots(figsize=(10.5,6.8)); ax.set_xlim(0,1600); ax.set_ylim(1060,0); ax.axis('off')
def box(x,y,w,h,t,bold=False,fill='white',fs=13):
    ax.add_patch(FancyBboxPatch((x,y),w,h,boxstyle='round,pad=0,rounding_size=14',fc=fill,ec='k',lw=1.4))
    ax.text(x+w/2,y+h/2,t,ha='center',va='center',fontsize=fs,fontweight='bold' if bold else 'normal',linespacing=1.3)
def arr(x1,y1,x2,y2,style='-|>',ls='-'):
    ax.annotate('',xy=(x2,y2),xytext=(x1,y1),arrowprops=dict(arrowstyle=style,color='k',lw=1.4,mutation_scale=14,linestyle=ls))
box(20,150,180,120,'QR sale',True)
box(250,150,290,120,'(i) Payment\nconfirmation\n(real time)',fs=12.5)
arr(204,210,244,210)
box(660,30,600,140,'(ii) Interbank clearing and settlement\nbetween institutions\n(scheme rules, e.g. next business day)',fs=11.5)
box(660,250,600,140,'(iii) Merchant payout: usable funds\nto the merchant\n(acquirer or aggregator terms)',False,'#e6e6e6',fs=12.5)
arr(544,195,654,110); arr(544,225,654,320)
ax.text(1280,210,'Legs (ii) and (iii)\nare timed separately:\npayout may precede\nor follow interbank\nsettlement',fontsize=12,style='italic',va='center',ha='left',linespacing=1.25)
# effective lag bracket from sale to payout
arr(40,440,1256,440,'<->')
ax.text(648,474,r'Effective settlement lag $\bar{L}$: sale to usable funds (payout arrangement, business-day calendar)',ha='center',va='center',fontsize=13)
arr(648,504,648,560)
box(408,566,480,110,'Can the merchant borrow\nagainst the float?')
arr(404,622,290,752); arr(892,622,1010,752)
ax.text(300,680,'Yes',style='italic',fontsize=14,ha='center'); ax.text(1000,680,'No',style='italic',fontsize=14,ha='center')
box(20,760,560,210,'Price channel\n'+r'$c = r\bar{L}/365$'+'\ncost per unit of QR sales,\ncomparable with the MDR')
box(700,760,560,210,'Quantity channel\n'+r'$\ell = q\bar{L}/(G+q\bar{L})$, $G>0$'+'\nmodel-implied reduction in\nsustainable sales capacity')
fig.tight_layout()
for ext in ('eps','pdf','svg'): fig.savefig(f'Fig1.{ext}')
fig.savefig('Fig1.png',dpi=600); plt.close(fig)
