/* Restaurant Order System — Table Shape SVGs
   These are rendered client-side so no PHP needed.
*/
window.ROS_SHAPES = (function(){
  'use strict';

  var TC_FREE     = '#C9A96E';  // gold — free table stroke
  var TC_OCC      = '#555';     // occupied
  var TC_MINE     = '#27AE60';  // my table
  var TC_RES      = '#9b59b6';  // reserved
  var CF          = 'rgba(255,255,255,0.12)'; // chair fill (dark bg)

  function rect(x,y,w,h,tc){ return "<rect x='"+x+"' y='"+y+"' width='"+w+"' height='"+h+"' rx='3' fill='rgba(255,255,255,0.06)' stroke='"+tc+"' stroke-width='1.8'/>"; }
  function oval(cx,cy,rx,ry,tc){ return "<ellipse cx='"+cx+"' cy='"+cy+"' rx='"+rx+"' ry='"+ry+"' fill='rgba(255,255,255,0.06)' stroke='"+tc+"' stroke-width='1.8'/>"; }
  function circle(cx,cy,r,tc){ return "<circle cx='"+cx+"' cy='"+cy+"' r='"+r+"' fill='rgba(255,255,255,0.06)' stroke='"+tc+"' stroke-width='1.8'/>"; }
  function chairH(cx,y,tc){ var x=cx-6; return "<rect x='"+x+"' y='"+y+"' width='12' height='7' rx='2' fill='"+CF+"' stroke='"+tc+"' stroke-width='1.2'/>"; }
  function chairV(x,cy,tc){ var y=cy-6; return "<rect x='"+x+"' y='"+y+"' width='7' height='12' rx='2' fill='"+CF+"' stroke='"+tc+"' stroke-width='1.2'/>"; }
  function chairDot(cx,cy,tc){ return "<circle cx='"+cx+"' cy='"+cy+"' r='4' fill='"+CF+"' stroke='"+tc+"' stroke-width='1.2'/>"; }
  function radialChairs(n,cx,cy,r,tc){
    var s='';
    for(var i=0;i<n;i++){
      var a = (i*(360/n)-90)*Math.PI/180;
      var ox = Math.round((cx+r*Math.cos(a))*10)/10;
      var oy = Math.round((cy+r*Math.sin(a))*10)/10;
      s += chairDot(ox,oy,tc);
    }
    return s;
  }

  function build(shape, tc){
    var s = "<svg viewBox='0 0 70 60' xmlns='http://www.w3.org/2000/svg'>";
    switch(shape){
      case 'rect_2':
        s+=rect(15,18,40,24,tc)+chairH(35,5,tc)+chairH(35,49,tc); break;
      case 'rect_4':
        s+=rect(10,15,50,30,tc)+chairH(22,3,tc)+chairH(48,3,tc)+chairH(22,51,tc)+chairH(48,51,tc); break;
      case 'rect_6':
        s+=rect(8,14,54,32,tc)+chairH(16,2,tc)+chairH(33,2,tc)+chairH(50,2,tc)+chairH(16,51,tc)+chairH(33,51,tc)+chairH(50,51,tc); break;
      case 'rect_8':
        s+=rect(8,14,54,32,tc)+chairH(16,2,tc)+chairH(33,2,tc)+chairH(50,2,tc)+chairH(16,51,tc)+chairH(33,51,tc)+chairH(50,51,tc)+chairV(0,21,tc)+chairV(59,21,tc); break;
      case 'rect_10':
        s+=rect(8,14,54,32,tc)+chairH(14,2,tc)+chairH(26,2,tc)+chairH(44,2,tc)+chairH(56,2,tc)+chairH(14,51,tc)+chairH(26,51,tc)+chairH(44,51,tc)+chairH(56,51,tc)+chairV(0,21,tc)+chairV(59,21,tc); break;
      case 'oval_4':
        s+=oval(35,30,28,16,tc)+chairH(22,3,tc)+chairH(48,3,tc)+chairH(22,51,tc)+chairH(48,51,tc); break;
      case 'oval_6':
        s+=oval(35,30,30,16,tc)+chairH(14,2,tc)+chairH(33,2,tc)+chairH(52,2,tc)+chairH(14,51,tc)+chairH(33,51,tc)+chairH(52,51,tc); break;
      case 'oval_8':
        s+=oval(35,30,30,16,tc)+chairH(14,2,tc)+chairH(33,2,tc)+chairH(52,2,tc)+chairH(14,51,tc)+chairH(33,51,tc)+chairH(52,51,tc)+chairV(1,22,tc)+chairV(59,22,tc); break;
      case 'round_4':
        s+=circle(35,30,16,tc)+chairH(27,3,tc)+chairH(27,50,tc)+chairV(1,23,tc)+chairV(57,23,tc); break;
      case 'round_6':
        s+=circle(35,30,17,tc)+radialChairs(6,35,30,28,tc); break;
      case 'round_8':
        s+=circle(35,30,17,tc)+radialChairs(8,35,30,27,tc); break;
      case 'round_10':
        s+=circle(35,30,17,tc)+radialChairs(10,35,30,27,tc); break;
      case 'square_4':
        s+=rect(16,14,38,32,tc)+chairH(27,2,tc)+chairH(27,51,tc)+chairV(1,23,tc)+chairV(57,23,tc); break;
      case 'u_10':
        s+="<rect x='8' y='8' width='14' height='44' rx='2' fill='rgba(255,255,255,0.05)' stroke='"+tc+"' stroke-width='1.8'/>"
          +"<rect x='48' y='8' width='14' height='44' rx='2' fill='rgba(255,255,255,0.05)' stroke='"+tc+"' stroke-width='1.8'/>"
          +"<rect x='8' y='44' width='54' height='14' rx='2' fill='rgba(255,255,255,0.05)' stroke='"+tc+"' stroke-width='1.8'/>"
          +chairV(0,14,tc)+chairV(0,30,tc)+chairV(59,14,tc)+chairV(59,30,tc)
          +chairH(16,55,tc)+chairH(30,55,tc)+chairH(44,55,tc)+chairH(58,55,tc);
        break;
      case 'u_12':
        s+="<rect x='8' y='6' width='14' height='46' rx='2' fill='rgba(255,255,255,0.05)' stroke='"+tc+"' stroke-width='1.8'/>"
          +"<rect x='48' y='6' width='14' height='46' rx='2' fill='rgba(255,255,255,0.05)' stroke='"+tc+"' stroke-width='1.8'/>"
          +"<rect x='8' y='44' width='54' height='14' rx='2' fill='rgba(255,255,255,0.05)' stroke='"+tc+"' stroke-width='1.8'/>"
          +chairV(0,10,tc)+chairV(0,24,tc)+chairV(0,38,tc)
          +chairV(59,10,tc)+chairV(59,24,tc)+chairV(59,38,tc)
          +chairH(14,56,tc)+chairH(26,56,tc)+chairH(38,56,tc)+chairH(50,56,tc)+chairH(62,56,tc);
        break;
      default: // fallback rect_4
        s+=rect(10,15,50,30,tc)+chairH(22,3,tc)+chairH(48,3,tc)+chairH(22,51,tc)+chairH(48,51,tc);
    }
    s += "</svg>";
    return s;
  }

  return {
    get: function(shape, state){
      var colors = {
        free:     '#C9A96E',
        occupied: '#E74C3C',
        mine:     '#27AE60',
        reserved: '#9b59b6',
        pending:  '#E67E22',
        accepting:'#2980B9',
        preparing:'#E67E22',
        ready:    '#27AE60',
      };
      var tc = colors[state] || colors.free;
      return build(shape||'rect_4', tc);
    }
  };
})();
