File size: 13,706 Bytes
cb5d9d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {
  type PointerEvent,
  type SyntheticEvent,
  type RefObject,
} from "react";
import {
  ArrowLeft,
  Share2,
  Download,
  MapPin,
  ShoppingCart,
  RefreshCw,
  RotateCcw,
  Paintbrush,
  Loader2,
} from "lucide-react";
import type { Product } from "../../types";
import type { SegmentMeta } from "../../hooks/useSegmentCanvas";

interface RoomPreviewPanelProps {
  previewImage?: string | null;
  offset: { x: number; y: number };
  zoom: number;
  imageSize: { width: number; height: number };
  wrapperRef: RefObject<HTMLDivElement | null>;
  canvasRef: RefObject<HTMLCanvasElement | null>;
  selectedProduct: Product | null;
  selectedMasks: Set<number>;
  hoveredMask: number;
  segmentMeta: Map<number, SegmentMeta>;
  isApplying: boolean;
  onBack: () => void;
  onPointerDown: (event: PointerEvent<HTMLDivElement>) => void;
  onPointerMove: (event: PointerEvent<HTMLDivElement>) => void;
  onPointerUp: (event: PointerEvent<HTMLDivElement>) => void;
  updateImageSize: (img: HTMLImageElement) => void;
  onCanvasMouseMove: (e: React.MouseEvent<HTMLCanvasElement>) => void;
  onCanvasMouseLeave: () => void;
  onCanvasClick: (e: React.MouseEvent<HTMLCanvasElement>) => void;
  onApplyTexture: () => void;
  onReset: () => void;
  onDownload: () => Promise<void>;
  onShare: () => Promise<void>;
}

export function RoomPreviewPanel({
  previewImage,
  offset,
  zoom,
  imageSize,
  wrapperRef,
  canvasRef,
  selectedProduct,
  selectedMasks,
  hoveredMask,
  segmentMeta,
  isApplying,
  onBack,
  onPointerDown,
  onPointerMove,
  onPointerUp,
  updateImageSize,
  onCanvasMouseMove,
  onCanvasMouseLeave,
  onCanvasClick,
  onApplyTexture,
  onReset,
  onDownload,
  onShare,
}: RoomPreviewPanelProps) {
  const canApply = selectedMasks.size > 0 && selectedProduct != null;

  const getLabel = (index: number) =>
    segmentMeta.get(index)?.label ?? `Zona ${index}`;

  return (
    <div className="w-full h-full bg-white overflow-hidden">
      <div className="relative h-full overflow-hidden lg:rounded-lg bg-[#f4f8ff]">

        {/* ── Mobile top bar ───────────────────────────────────────── */}
        {/* pr-12 reserva espacio a la derecha para el botón .hr-close del padre */}
        <div className="lg:hidden absolute left-0 right-0 top-0 z-10 h-12 bg-white shadow-sm flex items-center justify-between px-3 pr-12">
          <button
            onClick={onBack}
            className="p-2 rounded-full hover:bg-gray-100 transition-colors"
          >
            <ArrowLeft className="h-5 w-5 text-[#333]" />
          </button>
          <div className="flex items-center gap-1">
            <button
              onClick={onShare}
              className="p-2 rounded-full hover:bg-[#eaf1ff] transition-colors"
            >
              <Share2 className="h-5 w-5 text-[#0047AB]" />
            </button>
            <button
              onClick={onDownload}
              className="p-2 rounded-full hover:bg-[#eaf1ff] transition-colors"
            >
              <Download className="h-5 w-5 text-[#0047AB]" />
            </button>
          </div>
        </div>

        {/* ── Desktop top bar ──────────────────────────────────────── */}
        <div className="pointer-events-none hidden lg:flex absolute left-1/2 top-0 z-10 -translate-x-1/2 w-[80%] h-16 rounded-b-md bg-white shadow-sm items-center justify-center gap-4 px-3">
          <button
            onClick={onBack}
            className="pointer-events-auto rounded-full bg-[#333333] px-4 py-2 text-sm font-semibold text-white hover:bg-black flex items-center gap-2"
          >
            <ArrowLeft className="h-4 w-4" /> Cambiar de Habitación
          </button>
          <span className="text-gray-400">|</span>
          <button
            onClick={onShare}
            className="pointer-events-auto rounded-full bg-transparent px-4 py-2 text-sm font-medium text-[#0047AB] hover:bg-[#eaf1ff] flex items-center gap-2"
          >
            <Share2 className="h-4 w-4 text-[#0047AB]" />
            Compartir
          </button>
          <button
            onClick={onDownload}
            className="pointer-events-auto rounded-full bg-transparent px-4 py-2 text-sm font-medium text-[#0047AB] hover:bg-[#eaf1ff] flex items-center gap-2"
          >
            <Download className="h-4 w-4 text-[#0047AB]" /> Descargar
          </button>
          <a
            href="https://nauffargermany.com/gt/sucursales-2/"
            target="_blank"
            rel="noopener noreferrer"
            className="pointer-events-auto rounded-full bg-transparent px-4 py-2 text-sm font-medium text-[#0047AB] hover:bg-[#eaf1ff] flex items-center gap-2"
          >
            <MapPin className="h-4 w-4 text-[#0047AB]" /> Encuentra tu tienda
          </a>
          {selectedProduct?.detailUrl ? (
            <a
              href={selectedProduct.detailUrl}
              target="_blank"
              rel="noopener noreferrer"
              className="pointer-events-auto rounded-full bg-transparent px-4 py-2 text-sm font-medium text-[#0047AB] hover:bg-[#eaf1ff] flex items-center gap-2"
            >
              <ShoppingCart className="h-4 w-4 text-[#0047AB]" /> Ir a la página del producto
            </a>
          ) : (
            <button
              disabled
              className="pointer-events-auto rounded-full bg-transparent px-4 py-2 text-sm font-medium text-gray-300 flex items-center gap-2 cursor-default"
            >
              <ShoppingCart className="h-4 w-4 text-gray-300" /> Ir a la página del producto
            </button>
          )}
        </div>

        {/* ── Área de imagen + canvas ──────────────────────────────── */}
        <div
          ref={wrapperRef}
          className="absolute inset-x-0 top-12 lg:top-16 bottom-12 lg:bottom-16 flex items-center justify-center bg-[#edf4ff] overflow-hidden"
          onPointerDown={onPointerDown}
          onPointerMove={onPointerMove}
          onPointerUp={onPointerUp}
        >
          {previewImage ? (
            <div
              style={{
                transform: `translate(${offset.x}px, ${offset.y}px) scale(${zoom})`,
                transformOrigin: "center center",
                position: "relative",
                display: "inline-flex",
                lineHeight: 0,
                ...(imageSize.width > 0
                  ? { width: imageSize.width, height: imageSize.height }
                  : {}),
              }}
            >
              <img
                src={previewImage}
                alt="Vista previa de la habitación"
                draggable={false}
                onDragStart={(e) => e.preventDefault()}
                onLoad={(event: SyntheticEvent<HTMLImageElement>) =>
                  updateImageSize(event.currentTarget)
                }
                style={{
                  display: "block",
                  width: imageSize.width > 0 ? "100%" : "auto",
                  height: imageSize.height > 0 ? "100%" : "auto",
                  maxWidth: imageSize.width > 0 ? "none" : "100%",
                  maxHeight: imageSize.height > 0 ? "none" : "100%",
                  objectFit: "contain",
                }}
              />
              <canvas
                ref={canvasRef}
                style={{
                  position: "absolute",
                  inset: 0,
                  width: "100%",
                  height: "100%",
                  cursor: "crosshair",
                }}
                onMouseMove={onCanvasMouseMove}
                onMouseLeave={onCanvasMouseLeave}
                onClick={onCanvasClick}
              />
            </div>
          ) : (
            <div className="flex h-full w-full items-center justify-center text-[#707070] text-sm px-6 text-center">
              No hay vista previa disponible aún.
            </div>
          )}
        </div>

        {/* ── Hint de selección ────────────────────────────────────── */}
        {previewImage && selectedMasks.size === 0 && (
          <div className="pointer-events-none absolute top-14 lg:top-20 left-1/2 -translate-x-1/2 z-10 bg-black/50 text-white text-xs px-3 py-1.5 rounded-full whitespace-nowrap">
            {hoveredMask > 0
              ? `${getLabel(hoveredMask)} — haz clic para seleccionar`
              : "Haz clic sobre una zona de la imagen para seleccionarla"}
          </div>
        )}

        {/* ── Mobile bottom bar ────────────────────────────────────── */}
        <div className="pointer-events-none lg:hidden absolute left-0 right-0 bottom-0 z-10 h-12 bg-white border-t border-gray-100 shadow-sm flex items-center px-3 gap-2">
          {selectedProduct && (
            <div className="flex items-center gap-2 min-w-0 flex-1">
              <img
                src={selectedProduct.image}
                alt={selectedProduct.name}
                className="w-8 h-8 object-cover rounded-md border border-gray-200 shrink-0"
              />
              <div className="min-w-0">
                <p className="text-[10px] text-[#707070] truncate">{selectedProduct.brand}</p>
                <p className="text-xs font-semibold text-[#333] truncate leading-tight">{selectedProduct.name}</p>
              </div>
            </div>
          )}
          {!selectedProduct && selectedMasks.size > 0 && (
            <p className="text-xs text-[#0047AB] font-medium truncate flex-1">
              {[...selectedMasks].map(getLabel).join(", ")}
            </p>
          )}
          {!selectedProduct && selectedMasks.size === 0 && <div className="flex-1" />}

          <div className="flex items-center gap-1 ml-auto shrink-0">
            {canApply && (
              <button
                onClick={onApplyTexture}
                disabled={isApplying}
                className="pointer-events-auto flex items-center gap-1.5 bg-[#0047AB] text-white px-3 py-1.5 rounded-full text-xs font-semibold hover:bg-[#003a94] disabled:opacity-60 transition-colors"
              >
                {isApplying ? (
                  <Loader2 className="h-3 w-3 animate-spin" />
                ) : (
                  <Paintbrush className="h-3 w-3" />
                )}
                {isApplying ? "Aplicando..." : "Aplicar"}
              </button>
            )}
            <button
              onClick={onReset}
              className="pointer-events-auto p-2 rounded-full hover:bg-[#eaf1ff] transition-colors"
            >
              <RefreshCw className="h-4 w-4 text-[#0047AB]" />
            </button>
          </div>
        </div>

        {/* ── Desktop bottom bar ───────────────────────────────────── */}
        <div className="pointer-events-none hidden lg:flex absolute left-1/2 bottom-0 z-10 -translate-x-1/2 w-[80%] h-16 rounded-t-md bg-white border border-[#0047AB]/10 shadow-sm items-center justify-start gap-4 px-4">
          {selectedProduct && (
            <div className="pointer-events-none flex items-center gap-3">
              <img
                src={selectedProduct.image}
                alt={selectedProduct.name}
                className="w-10 h-10 object-cover rounded-md border border-gray-200"
              />
              <div>
                <p className="text-[#707070] text-xs">{selectedProduct.brand}</p>
                <p className="font-semibold text-[#333333] text-sm leading-tight">
                  {selectedProduct.name}
                </p>
              </div>
            </div>
          )}

          {selectedMasks.size > 0 && (
            <p className="text-xs text-[#0047AB] font-medium truncate max-w-[260px]">
              {[...selectedMasks].map(getLabel).join(", ")}
            </p>
          )}

          <div className="flex-1" />

          {canApply && (
            <button
              onClick={onApplyTexture}
              disabled={isApplying}
              className="pointer-events-auto flex items-center gap-2 bg-[#0047AB] text-white px-4 py-2 rounded-full text-sm font-semibold hover:bg-[#003a94] disabled:opacity-60 transition-colors"
            >
              {isApplying ? (
                <Loader2 className="h-4 w-4 animate-spin" />
              ) : (
                <Paintbrush className="h-4 w-4" />
              )}
              {isApplying ? "Aplicando..." : "Aplicar textura"}
            </button>
          )}

          <button
            onClick={onReset}
            className="pointer-events-auto rounded-full bg-transparent px-4 py-2 text-sm font-medium text-[#0047AB] hover:bg-[#eaf1ff] flex items-center gap-2"
          >
            <span className="inline-flex items-center justify-center rounded-full bg-[#eaf1ff] p-2">
              <RefreshCw className="h-4 w-4 text-[#0047AB]" />
            </span>
            Reiniciar
          </button>
          <button className="pointer-events-auto rounded-full bg-transparent px-4 py-2 text-sm font-medium text-[#0047AB] hover:bg-[#eaf1ff] flex items-center gap-2">
            <span className="inline-flex items-center justify-center rounded-full bg-[#eaf1ff] p-2">
              <RotateCcw className="h-4 w-4 text-[#0047AB]" />
            </span>
            Girar
          </button>
        </div>

      </div>
    </div>
  );
}