all files / lib/html-parsers/ scenario_edition.js

100% Statements 25/25
65% Branches 39/60
96.08% Functions 49/51
100% Lines 25/25
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
'use strict'
 
import trumpet from 'trumpet'
import { trumpetAttr, trumpetInnerText, trumpetAffectValue } from './index'
 
/**
 * Returns a trumpet parser that will get input values to go to the next edition step.
 *
 * GET https://myfox.me/scenario/{homeId}/manage/{scenarioId}/1
 * => <form id="scenarioForm" action="https://myfox.me/scenario/xxxx/manage/xxxx/2" method="post">
 *      <input type="hidden" name="scData" value="xxxx" />
 *      <input type="text" name="label" id="scenarioLabel" value="xxxxx" maxlength="30" autocomplete="off" />
 *    </form>
 *
 * @param  {MyfoxWrapperApiCommon}  wrapperApi  The Api instance, to throw events
 * @return {trumpet}                The trumpet parser to plug on the HTML stream.
 */
export function step1Parser (wrapperApi) {
  const trumpetParser = trumpet()
  trumpetParser.nextPayload = {}
 
  // get scData
  trumpetParser.select('form#scenarioForm input[name="scData"]', trumpetAttr('value', (value) => {
    trumpetParser.nextPayload.scData = value
  }))
  // get label
  trumpetParser.select('form#scenarioForm input[name="label"]', trumpetAttr('value', (value) => {
    trumpetParser.nextPayload.label = value
  }))
 
  // input submit (button) means end of the parsing
  trumpetParser.select('form#scenarioForm input[type="submit"]', () => {
    Iif (!trumpetParser.nextPayload.scData || !trumpetParser.nextPayload.label) {
      const error = new Error('Scenario edition failed at step 1. Step 1 form not found.')
      error.status = 404
      return trumpetParser.emit('error', error)
    }
    trumpetParser.end()
  })
 
  return trumpetParser
}
 
/**
 * Returns a trumpet parser that will get input values to go to the next edition step.
 *
 * POST https://myfox.me/scenario/{homeId}/manage/{scenarioId}/2
 *    with scData: "xxxx"
 *         label: "xxxxx"
 * => <form id="scenarioForm" action="https://myfox.me/scenario/xxxx/manage/xxxx/3" method="post">
 *      <input type="hidden" name="scData" value="xxxx" />
 *      <input type="hidden" id="scenarioType" name="type" value="xxxx" />
 *    </form>
 *
 * @param  {MyfoxWrapperApiCommon}  wrapperApi  The Api instance, to throw events
 * @return {trumpet}                The trumpet parser to plug on the HTML stream.
 */
export function step2Parser (wrapperApi) {
  const trumpetParser = trumpet()
  trumpetParser.nextPayload = {}
 
  // get scData
  trumpetParser.select('form#scenarioForm input[name="scData"]', trumpetAttr('value', (value) => {
    trumpetParser.nextPayload.scData = value
  }))
  // get label
  trumpetParser.select('form#scenarioForm input[name="type"]', trumpetAttr('value', (value) => {
    trumpetParser.nextPayload.type = value
  }))
 
  // input submit (button) means end of the parsing
  trumpetParser.select('form#scenarioForm input[type="submit"]', () => {
    Iif (!trumpetParser.nextPayload.scData || !trumpetParser.nextPayload.type) {
      const error = new Error('Scenario edition failed at step 2. Step 2 form not found.')
      error.status = 404
      return trumpetParser.emit('error', error)
    }
    trumpetParser.end()
  })
 
  return trumpetParser
}
 
/**
 * Returns a trumpet parser that will inspect form structure searching for temperature condition occurrences.
 *
 * POST https://myfox.me/scenario/{homeId}/manage/{scenarioId}/3
 *    with scData: "xxxx"
 *         type: "xxxx"
 * => <form id="scenarioForm" action="https://myfox.me/scenario/xxxx/manage/xxxx/4" method="post">
 *      <input type="hidden" name="scData" value="xxxx" />
 *      <inputs ...>  (not needed here)
 *      <div class="tab-content tab-sensors">
 *        <input type="radio" name="_trigger_type" value="4" checked="checked" />   (if checked, keep condition)
 *        <div class="settings-tooltip" id="_trigger_4">
 *          <div class="settings temperature">
 *            <select name="_trigger[4][1][deviceSlaveId]"><option value="xxxx" selected="selected">Capteur SdB</option><option value="xxxx">Capteur Salon</option></select>
 *            <input name="_trigger[4][1][value]" value="15" type="text" class="" />
 *            <input type="radio" name="_trigger[4][1][operator]" value="1" checked="checked" /> (à la hausse)
 *            <input type="radio" name="_trigger[4][1][operator]" value="0" /> (à la baisse)
 *          </div>
 *        </div>
 *      </div>
 *      <div class="conditions">
 *        <div class="condition condition-4 option-block" data-triggertype="4">
 *          <input type="checkbox" name="_condition_4_1" value="4" />
 *          <span>Si la température...</span>
 *        </div>
 *        <div class="condition-inputs choice-4-1">
 *          ... du capteur 	<select name="_condition[4][1][deviceSlaveId]">
 *            <option value="xxxx">Capteur SdB</option>
 *            <option value="xxxx" selected="selected">Capteur Salon</option>
 *          </select> est 	<select name="_condition[4][1][operator]">
 *            <option value="0" selected="selected"><</option>
 *            <option value="1">></option>
 *            <option value="2">=</option>
 *          </select> à <input name="_condition[4][1][value]" value="16" type="text" class="" /> °C
 *        </div>
 *        <div class="condition choice-4-2">
 *          <input type="checkbox" name="_condition_4_2" value="4" />
 *          <span>Et si la température...</span>
 *        </div>
 *        <div class="item condition-inputs">
 *          ... du capteur 	<select name="_condition[4][2][deviceSlaveId]">
 *            <option value="xxxx">Capteur SdB</option>
 *            <option value="xxxx" selected="selected">Capteur Salon</option>
 *          </select> est 	<select name="_condition[4][2][operator]">
 *            <option value="0"><</option>
 *            <option value="1" selected="selected">></option>
 *            <option value="2">=</option>
 *          </select> à <input name="_condition[4][2][value]" value="14" type="text" class="" /> °C
 *        </div>
 *      </div>
 *    </form>
 *
 * @param  {MyfoxWrapperApiCommon}  wrapperApi  The Api instance, to throw events
 * @return {trumpet}                The trumpet parser to plug on the HTML stream.
 */
export function step3TempInspectionParser (wrapperApi) {
  const trumpetParser = trumpet()
  trumpetParser.data = {}
  trumpetParser.setMaxListeners(14) // We have a lot of stuff to analyze...
 
  // if input type="radio" name="_trigger_type" value="4" has checked="checked", then scan main temperature condition
  trumpetParser.select('form#scenarioForm input[name="_trigger_type"][value="4"][checked="checked"]', (element) => {
    trumpetParser.data.trigger_4 = {}
 
    // get scenario
    trumpetParser.select(
      'form#scenarioForm select[name="_trigger[4][1][deviceSlaveId]"] option[selected="selected"]',
      trumpetAttr('value', (value) => {
        trumpetParser.data.trigger_4 = {deviceSlaveId: value}
      })
    )
    trumpetParser.select(
      'form#scenarioForm select[name="_trigger[4][1][deviceSlaveId]"] option[selected="selected"]',
      trumpetInnerText((text) => {
        trumpetParser.data.trigger_4.deviceSlaveName = text
      })
    )
 
    // get temperature value
    trumpetParser.select(
      'form#scenarioForm input[name="_trigger[4][1][value]"]',
      trumpetAttr('value', (value) => {
        trumpetParser.data.trigger_4.value = value
      })
    )
 
    // get operator
    trumpetParser.select(
      'form#scenarioForm input[name="_trigger[4][1][operator]"][checked="checked"]',
      trumpetAttr('value', (value) => {
        trumpetParser.data.trigger_4.operator = value
      })
    )
  })
 
  // First secondary condition
  trumpetParser.select(
    'div.conditions div.choice-4-1 select[name="_condition[4][1][deviceSlaveId]"] option[selected="selected"]',
    trumpetAttr('value', (value) => {
      trumpetParser.data.condition_4_1 = {deviceSlaveId: value}
    })
  )
  trumpetParser.select(
    'div.conditions div.choice-4-1 select[name="_condition[4][1][deviceSlaveId]"] option[selected="selected"]',
    trumpetInnerText((text) => {
      trumpetParser.data.condition_4_1.deviceSlaveName = text
    })
  )
  trumpetParser.select(
    'div.conditions div.choice-4-1 select[name="_condition[4][1][operator]"] option[selected="selected"]',
    trumpetAttr('value', (value) => {
      Eif (trumpetParser.data.condition_4_1) {
        trumpetParser.data.condition_4_1.operator = value
      }
    })
  )
  trumpetParser.select(
    'div.conditions div.choice-4-1 input[name="_condition[4][1][value]"]',
    trumpetAttr('value', (value) => {
      Eif (trumpetParser.data.condition_4_1) {
        trumpetParser.data.condition_4_1.value = value
      }
    })
  )
 
  // Second secondary condition
  trumpetParser.select(
    'div.conditions div.item.condition-inputs select[name="_condition[4][2][deviceSlaveId]"] option[selected="selected"]',
    trumpetAttr('value', (value) => {
      trumpetParser.data.condition_4_2 = {deviceSlaveId: value}
    })
  )
  trumpetParser.select(
    'div.conditions div.item.condition-inputs select[name="_condition[4][2][deviceSlaveId]"] option[selected="selected"]',
    trumpetInnerText((text) => {
      trumpetParser.data.condition_4_2.deviceSlaveName = text
    })
  )
  trumpetParser.select(
    'div.conditions div.item.condition-inputs select[name="_condition[4][2][operator]"] option[selected="selected"]',
    trumpetAttr('value', (value) => {
      Eif (trumpetParser.data.condition_4_2) {
        trumpetParser.data.condition_4_2.operator = value
      }
    })
  )
  trumpetParser.select(
    'div.conditions div.item.condition-inputs input[name="_condition[4][2][value]"]',
    trumpetAttr('value', (value) => {
      Eif (trumpetParser.data.condition_4_2) {
        trumpetParser.data.condition_4_2.value = value
      }
    })
  )
 
  return trumpetParser
}
 
/**
 * Returns a trumpet parser that will inspect and modify form structure searching for temperature condition occurrences.
 *
 * POST https://myfox.me/scenario/{homeId}/manage/{scenarioId}/3
 *    with scData: "xxxx"
 *         type: "xxxx"
 * => <form id="scenarioForm" action="https://myfox.me/scenario/xxx/manage/xxxxxx/4" method="post">
 *      <input type="hidden" name="scData" value="xxxxx" />
 *      <h2>...</h2>
 *      <div class="tabs-list">...</div>
 *      <div class="tabs-contents">
 *
 *        <div class="tab-content tab-securityLevel">
 *          <div class="item security-level">
 *            <ul class="...">
 *              <li class="seclev-4">
 *                <span ...><span ...>
 *                  <input type="checkbox" name="_trigger[3][4]" value="1" />
 *                </span></span>
 *              </li>
 *              <li class="seclev-2">
 *                <span ...><span ...>
 *                  <input type="checkbox" name="_trigger[3][2]" value="1" />
 *                </span></span>
 *              </li>
 *              <li class="seclev-1">
 *                <span ...><span ...>
 *                  <input type="checkbox" name="_trigger[3][1]" value="1" />
 *                </span></span>
 *              </li>
 *            </ul>
 *          </div>
 *        </div>
 *
 *        <div class="tab-content tab-risks">
 *          <div class="item">
 *            <ul class="reset">
 *              <li class="risk-10 option-block">
 *                <span ...><span ...>
 *                    <input type="radio" name="_trigger[2]" value="10" />
 *                </span></span>
 *              </li>
 *              <li class="risk-16 option-block">
 *                <span ...><span ...>
 *                  <input type="radio" name="_trigger[2]" value="16" />
 *                </span></span>
 *              </li>
 *              <li class="risk-4 option-block">
 *                <span ...><span ...>
 *                  <input type="radio" name="_trigger[2]" value="4" />
 *                </span></span>
 *              </li>
 *              <li class="risk-512 option-block">
 *                <span ...><span ...>
 *                  <input type="radio" name="_trigger[2]" value="512" />
 *                </span></span>
 *              </li>
 *              <li class="risk-2048 option-block">
 *                <span ...><span ...>
 *                  <input type="radio" name="_trigger[2]" value="2048" />
 *                </span></span>
 *              </li>
 *            </ul>
 *          </div>
 *        </div>
 *
 *        <div class="tab-content tab-sensors"><div class="item">
 *          <ul class="reset">
 *            <li class="sensors sensors-4 option-block" data-triggertype="4">
 *              <div class="choice">
 *                <span ...><span ...>
 *                  <input type="radio" name="_trigger_type" value="4" checked="checked" />
 *                </span></span>
 *              </div>
 *              <div class="settings-tooltip" id="_trigger_4">
 *                <div class="settings-tooltip-body">
 *                  <div class="settings temperature">
 *                      <table>
 *                        <tr><td>Capteur</td><td>
 *                          <select name="_trigger[4][1][deviceSlaveId]">
 *                            <option value="xxxxxx" selected="selected">Capteur SdB</option><option value="xxxxxx">Capteur Salon</option>
 *                          </select>
 *                        </td></tr><tr><td>Température</td><td><span class="input input-temperature">
 *                          <input name="_trigger[4][1][value]" value="15" type="text" class="" />
 *                        </span> °C	</td></tr><tr><td>Franchissement</td><td><span class="input-radio " data-default="1" data-group="_trigger_4__1__operator_" data-cancel="0">
 *                          <span class="icon">
 *                            <input type="radio" name="_trigger[4][1][operator]" value="1" checked="checked" />
 *                          </span>
 *                        </span> à la hausse &nbsp; &nbsp; <span class="input-radio " data-default="1" data-group="_trigger_4__1__operator_" data-cancel="0">
 *                          <span class="icon">
 *                            <input type="radio" name="_trigger[4][1][operator]" value="0" />
 *                          </span>
 *                        </span> à la baisse </td></tr>
 *                      </table>
 *                  </div>
 *                </div><!-- End tooltip body -->
 *              </div>
 *            </li>
 *            <li class="sensors sensors-5 option-block" data-triggertype="5">
 *              <div class="choice">
 *                <span class="input-radio trigger-choice" data-default="" data-group="_trigger_type" data-cancel="1"><span class="icon">
 *                  <input type="radio" name="_trigger_type" value="5" />
 *                </span></span>
 *              </div>
 *              <div class="settings-tooltip" id="_trigger_5">
 *                <div class="settings-tooltip-body">
 *                  <div class="settings light">
 *                      <table>
 *                        <tr><td>Capteur</td><td>
 *                        <select name="_trigger[5][1][deviceSlaveId]"><option value="xxxxxx">Capteur SdB</option><option value="xxxxxxx">Capteur Salon</option></select>
 *                        </td></tr><tr><td>Luminosité</td><td>
 *                          <select name="_trigger[5][1][value]">
 *                            <option value="1">Pleine lumière</option>
 *                            <option value="2">Lumière du jour</option>
 *                            <option value="4">Lumière basse</option>
 *                            <option value="5">Pénombre</option>
 *                            <option value="6">Obscurité</option>
 *                          </select>
 *                        </td></tr><tr><td>Franchissement</td><td><span class="input-radio " data-default="1" data-group="_trigger_5__1__operator_" data-cancel="0"><span class="icon">
 *                          <input type="radio" name="_trigger[5][1][operator]" value="1" checked="checked" />
 *                        </span></span> à la hausse &nbsp; &nbsp; <span class="input-radio " data-default="1" data-group="_trigger_5__1__operator_" data-cancel="0"><span class="icon">
 *                          <input type="radio" name="_trigger[5][1][operator]" value="0" />
 *                        </span></span> à la baisse </td></tr>
 *                      </table>
 *                  </div>
 *                </div><!-- End tooltip body -->
 *              </div>
 *            </li>
 *            <li class="sensors sensors-7 option-block" data-triggertype="7">
 *              <div class="choice">
 *                <span class="input-radio trigger-choice" data-default="" data-group="_trigger_type" data-cancel="1"><span class="icon">
 *                  <input type="radio" name="_trigger_type" value="7" />
 *                </span></span>
 *              </div>
 *              <div class="settings-tooltip" id="_trigger_7">
 *                <div class="settings-tooltip-body">
 *                  <div class="settings device">
 *                      <table>
 *                      <tr><td>Capteur</td><td>
 *                      <select name="_trigger[7]">
 *                        <option value="xxx">Porte d'entrée</option>
 *                        <option value="xxxx">Mouvement garage</option>
 *                        <option value="xxxx">Fenêtre salon</option>
 *                        <option value="xxxx">Mouvement salon</option>
 *                        <option value="xx">Porte du garage</option>
 *                      </select>
 *                      </td></tr>
 *                      </table>
 *                  </div>
 *                </div><!-- End tooltip body -->
 *              </div>
 *            </li>
 *          </ul>
 *        </div>
 *
 *      </div>
 *
 *      <div class="form-buttons collapse">
 *        <a href="#" class="btn-nextStep" >
 *          <input type="submit" name="sbt" value="Étape suivante" /><span class="text">Étape suivante</span>
 *        </a>
 *      </div>
 *
 *      <h2>Choisissez des conditions facultatives</h2>
 *      <div class="conditions">
 *
 *        <div class="condition condition-3 option-block" data-triggertype="3">
 *          <div class="title">
 *            <span class="input-checkbox condition-choice condition-3-choice" data-default=""><span class="icon">
 *              <input type="checkbox" name="_condition_3" value="3" />
 *            </span></span>
 *          </div>
 *          <div class="item condition-inputs security-level">
 *            <ul class="reset">
 *              <li class="seclev-4">
 *                <span class="input-radio " data-default="" data-group="trigger-3" data-cancel="1"><span class="icon">
 *                  <input type="radio" name="_condition[3][4]" value="1" />
 *                </span></span>
 *              </li>
 *              <li class="seclev-2">
 *                <span class="input-radio " data-default="" data-group="trigger-3" data-cancel="1"><span class="icon">
 *                  <input type="radio" name="_condition[3][2]" value="1" />
 *                </span></span>
 *              </li>
 *              <li class="seclev-1">
 *                <span class="input-radio " data-default="" data-group="trigger-3" data-cancel="1"><span class="icon">
 *                  <input type="radio" name="_condition[3][1]" value="1" />
 *                </span></span>
 *              </li>
 *            </ul>
 *          </div>
 *        </div>
 *
 *        <div class="condition condition-1 option-block" data-triggertype="1">
 *          <div class="title">
 *            <span class="input-checkbox condition-choice condition-1-choice" data-default=""><span class="icon">
 *              <input type="checkbox" name="_condition_1" value="1" />
 *            </span></span>
 *          </div>
 *          <div class="item condition-inputs">
 *            <div class="scenario-schedule scenario-schedule-range" data-field="scheduleRule">
 *              <table>...</table>
 *              <div class="ws-prompt input-disabled">
 *                <div class="ws-prompt-inner">
 *                  <div class="ws-prompt-row">
 *                      <table>
 *                        <tr><th><label for="schedule1">Heure de début</label></th><td><span class="input input-time" id="input-schedule1"><span class="icon"></span>
 *                          <input id="schedule1" name="time1" class="ws-prompt-time" placeholder="HH:mm" type="text" />
 *                        </span></td></tr><tr><th><label for="schedule2">Heure de fin</label></th><td><span class="input input-time" id="input-schedule2"><span class="icon"></span>
 *                          <input id="schedule2" name="time2" class="ws-prompt-time" placeholder="HH:mm" type="text" />
 *                        </span></td></tr>
 *                      </table>
 *                  </div>
 *                  <input type="hidden" name="day" />
 *                  <input type="hidden" name="idx" />
 *                </div>
 *              </div>
 *              <input type="hidden" name="_condition[1]" id="scheduleRule" value="" />
 *            </div>
 *          </div>
 *        </div>
 *
 *        <div class="condition condition-4 option-block" data-triggertype="4">
 *          <div class="title">
 *            <span class="input-checkbox condition-choice condition-Limited 4-1" data-default="4"><span class="icon">
 *              <input type="checkbox" name="_condition_4_1" value="4" />
 *            </span></span>
 *            <span>Si la température...</span>
 *          </div>
 *          <div class="item">
 *            <div class="condition-inputs choice-4-1">... du capteur
 *              <select name="_condition[4][1][deviceSlaveId]">
 *                <option value="xxx">Capteur SdB</option>
 *                <option value="xxx" selected="selected">Capteur Salon</option>
 *              </select>
 *              <select name="_condition[4][1][operator]">
 *                <option value="0" selected="selected"><</option>
 *                <option value="1">></option>
 *                <option value="2">=</option>
 *              </select>
 *              à <span class="input input-temperature">
 *                <input name="_condition[4][1][value]" value="16" type="text" class="" />
 *              </span>°C.
 *            </div>
 *            <div class="condition choice-4-2">
 *              <div class="title">
 *                <span class="input-checkbox condition-choice condition-Limited 4-2" data-default="4"><span class="icon">
 *                  <input type="checkbox" name="_condition_4_2" value="4" />
 *                </span></span>
 *                <span>Et si la température...</span>
 *              </div>
 *              <div class="item condition-inputs">... du capteur
 *                <select name="_condition[4][2][deviceSlaveId]">
 *                  <option value="xxx">Capteur SdB</option>
 *                  <option value="xxx" selected="selected">Capteur Salon</option>
 *                </select>
 *                <select name="_condition[4][2][operator]">
 *                  <option value="0"><</option>
 *                  <option value="1" selected="selected">></option>
 *                  <option value="2">=</option>
 *                </select>
 *                à <span class="input input-temperature">
 *                  <input name="_condition[4][2][value]" value="14" type="text" class="" />
 *                </span>°C.
 *              </div>
 *            </div>
 *          </div>
 *        </div>
 *
 *        <div class="condition condition-5 option-block" data-triggertype="5">
 *          <div class="title">
 *            <span class="input-checkbox condition-choice condition-Limited 5-1" data-default=""><span class="icon">
 *              <input type="checkbox" name="_condition_5_1" value="5" />
 *            </span></span>
 *            <span>Si la luminosité...</span>
 *          </div>
 *          <div class="item">
 *            <div class="condition-inputs choice-5-1"> ... du capteur
 *              <select name="_condition[5][1][deviceSlaveId]">
 *                <option value="xxx">Capteur SdB</option>
 *                <option value="xxx">Capteur Salon</option>
 *              </select>
 *              <select name="_condition[5][1][operator]">
 *                <option value="0" selected="selected"><</option>
 *                <option value="1">></option>
 *                <option value="2">=</option>
 *              </select>
 *              <select name="_condition[5][1][value]">
 *                <option value="1">Pleine lumière</option>
 *                <option value="2">Lumière du jour</option>
 *                <option value="4">Lumière basse</option>
 *                <option value="5">Pénombre</option>
 *                <option value="6">Obscurité</option>
 *              </select>
 *            </div>
 *            <div class="condition choice-5-2">
 *              <div class="title">
 *                <span class="input-checkbox condition-choice condition-Limited 5-2" data-default=""><span class="icon">
 *                  <input type="checkbox" name="_condition_5_2" value="5" />
 *                </span></span>
 *              </div>
 *              <div class="item condition-inputs"> ... du capteur
 *                <select name="_condition[5][2][deviceSlaveId]">
 *                  <option value="xxx">Capteur SdB</option>
 *                  <option value="xxx">Capteur Salon</option>
 *                </select>
 *                <select name="_condition[5][2][operator]">
 *                  <option value="0" selected="selected"><</option>
 *                  <option value="1">></option>
 *                  <option value="2">=</option>
 *                </select>
 *                <select name="_condition[5][2][value]">
 *                  <option value="1">Pleine lumière</option>
 *                  <option value="2">Lumière du jour</option>
 *                  <option value="4">Lumière basse</option>
 *                  <option value="5">Pénombre</option>
 *                  <option value="6">Obscurité</option>
 *                </select>
 *              </div>
 *            </div>
 *          </div>
 *        </div>
 *
 *      </div>
 *
 *      <div class="form-buttons">
 *        <input type="submit" name="sbt" value="Étape suivante" />
 *      </div>
 *    </form>
 *
 * =======================================
 * Form elements are classified like this:
 * =======================================
 *
 * <input type="hidden" name="scData" value="xxxxx" />
 * Trigger alarm :
 *   <input type="checkbox" name="_trigger[3][4]" value="1" />
 *   <input type="checkbox" name="_trigger[3][2]" value="1" />
 *   <input type="checkbox" name="_trigger[3][1]" value="1" />
 * Trigger risks :
 *   <input type="radio" name="_trigger[2]" value="10" /> x5
 * Trigger sensors 4:
 *   <input type="radio" name="_trigger_type" value="4" checked="checked" />
 *   <select name="_trigger[4][1][deviceSlaveId]">
 *     <option value="xxxxxx" selected="selected">Capteur SdB</option>
 *     ....
 *   </select>
 *   <input name="_trigger[4][1][value]" value="15" type="text" class="" />
 *   <input type="radio" name="_trigger[4][1][operator]" value="1" checked="checked" />
 *   <input type="radio" name="_trigger[4][1][operator]" value="0" />
 * Trigger sensors 5 :
 *   <input type="radio" name="_trigger_type" value="5" />
 *   <select name="_trigger[5][1][deviceSlaveId]">
 *     <option value="xxxxxx">Capteur SdB</option>
 *     ...
 *   </select>
 *   <select name="_trigger[5][1][value]">
 *     <option value="1">Pleine lumière</option>
 *     ...
 *     <option value="6">Obscurité</option>
 *   </select>
 *   <input type="radio" name="_trigger[5][1][operator]" value="1" checked="checked" />
 *   <input type="radio" name="_trigger[5][1][operator]" value="0" />
 * Trigger sensors 7 :
 *   <input type="radio" name="_trigger_type" value="7" />
 *   <select name="_trigger[7]">
 *     <option value="xxx">Porte d'entrée</option>
 *     ...
 *   </select>
 * CONDITIONS :
 *   <div class="conditions">
 *     Alarm:
 *       <span class="input-checkbox condition-choice condition-3-choice" data-default="3">
 *         <input...>  <-- don't care, see data-default in above span instead
 *       </span>
 *       <input type="radio" name="_condition[3][4]" value="1" />
 *       <input type="radio" name="_condition[3][2]" value="1" />
 *       <input type="radio" name="_condition[3][1]" value="1" />
 *
 *     Time conditions:
 *       <span class="input-checkbox condition-choice condition-1-choice" data-default="1">
 *         <input...>  <-- don't care, see data-default in above span instead
 *       </span>
 *       <input type="hidden" name="_condition[1]" id="scheduleRule" value="" />
 *       Not useful to save:
 *         <input id="schedule1" name="time1" class="ws-prompt-time" placeholder="HH:mm" type="text" />
 *         <input id="schedule2" name="time2" class="ws-prompt-time" placeholder="HH:mm" type="text" />
 *         <input type="hidden" name="day" />
 *         <input type="hidden" name="idx" />
 *
 *     Temperature conditions:
 *       <span class="input-checkbox condition-choice condition-Limited 4-1" data-default="4">
 *         <input ...>  <-- don't care, see data-default in above span instead
 *       </span>
 *       <select name="_condition[4][1][deviceSlaveId]">
 *         <option value="xxx" selected="selected">Capteur Salon</option>
 *       </select>
 *       <select name="_condition[4][1][operator]">
 *         <option value="0" selected="selected"><</option>
 *         <option value="1">></option>
 *         <option value="2">=</option>
 *       </select>
 *       <input name="_condition[4][1][value]" value="16" type="text" class="" />
 *       <span class="input-checkbox condition-choice condition-Limited 4-2" data-default="4">
 *         <input ...>  <-- don't care, see data-default in above span instead
 *       </span>
 *       <select name="_condition[4][2][deviceSlaveId]">
 *         <option value="xxx" selected="selected">Capteur Salon</option>
 *       </select>
 *       <select name="_condition[4][2][operator]">
 *         <option value="0"><</option>
 *         <option value="1" selected="selected">></option>
 *         <option value="2">=</option>
 *       </select>
 *       <input name="_condition[4][2][value]" value="14" type="text" class="" />
 *
 *     Lightning conditions:
 *       <span class="input-checkbox condition-choice condition-Limited 5-1" data-default="5">
 *         <input ...>  <-- don't care, see data-default in above span instead
 *       </span>
 *       <select name="_condition[5][1][deviceSlaveId]">
 *         <option value="xxx">Capteur SdB</option>
 *       </select>
 *       <select name="_condition[5][1][operator]">
 *         <option value="0" selected="selected"><</option>
 *         <option value="1">></option>
 *         <option value="2">=</option>
 *       </select>
 *       <select name="_condition[5][1][value]">
 *         <option value="1">Pleine lumière</option>
 *         ...
 *         <option value="6">Obscurité</option>
 *       </select>
 *       <span class="input-checkbox condition-choice condition-Limited 5-2" data-default="5">
 *         <input ...>  <-- don't care, see data-default in above span instead
 *       </span>
 *       <select name="_condition[5][2][deviceSlaveId]">
 *         <option value="xxx">Capteur SdB</option>
 *       </select>
 *       <select name="_condition[5][2][operator]">
 *         <option value="0" selected="selected"><</option>
 *         <option value="1">></option>
 *         <option value="2">=</option>
 *       </select>
 *       <select name="_condition[5][2][value]">
 *         <option value="1">Pleine lumière</option>
 *         ...
 *         <option value="6">Obscurité</option>
 *       </select>
 *   </div>
 * Submit:
 *   <input type="submit" name="sbt" value="Étape suivante" />
 *
 *
 *
 * @param  {MyfoxWrapperApiCommon}  wrapperApi  The Api instance, to throw events
 * @return {trumpet}                The trumpet parser to plug on the HTML stream.
 */
export function step3TempModificationParser (wrapperApi) {
  const trumpetParser = trumpet()
  trumpetParser.nextPayload = {}
  trumpetParser.setMaxListeners(30) // We have a lot of stuff to analyze...
 
  // get scData ✔
  trumpetParser.select(
    'form#scenarioForm input[name="scData"]',
    trumpetAffectValue(trumpetParser.nextPayload)
  )
 
  // Alarm trigger ✔
  trumpetParser.selectAll('form#scenarioForm input[type="checkbox"][name^="_trigger[3]"][checked="checked"]', trumpetAttr('name', (name) => {
    trumpetParser.nextPayload[name] = '1'
  }))
 
  // Risks trigger ✔
  trumpetParser.select(
    'form#scenarioForm input[type="radio"][name="_trigger[2]"][checked="checked"]',
    trumpetAffectValue(trumpetParser.nextPayload)
  )
 
  // Sensor 4 trigger (temperatures) OR Sensor 5 trigger (lightning) -> same structure ✔ ✔
  trumpetParser.select('form#scenarioForm input[type="radio"][name="_trigger_type"][checked="checked"]', trumpetAttr('value', (triggerType) => {
    trumpetParser.nextPayload['_trigger_type'] = triggerType
    // tab is selected, add more parsers
 
    // get scenario ✔
    trumpetParser.select(
      `form#scenarioForm select[name="_trigger[${triggerType}][1][deviceSlaveId]"] option[selected="selected"]`,
      trumpetAffectValue(trumpetParser.nextPayload, `_trigger[${triggerType}][1][deviceSlaveId]`)
    )
 
    // get temperature value ✔
    trumpetParser.select(
      `form#scenarioForm input[name="_trigger[${triggerType}][1][value]"]`,
      trumpetAffectValue(trumpetParser.nextPayload, `_trigger[${triggerType}][1][value]`)
    )
 
    // get lightning value ✔
    trumpetParser.select(
      `form#scenarioForm select[name="_trigger[${triggerType}][1][value]"] option[selected="selected"]`,
      trumpetAffectValue(trumpetParser.nextPayload, `_trigger[${triggerType}][1][value]`)
    )
 
    // get operator ✔
    trumpetParser.select(
      `form#scenarioForm input[name="_trigger[${triggerType}][1][operator]"][checked="checked"]`,
      trumpetAffectValue(trumpetParser.nextPayload, `_trigger[${triggerType}][1][operator]`)
    )
  }))
 
  // Sensor 7 trigger (intrusion) ✔
  trumpetParser.select('form#scenarioForm input[type="radio"][name="_trigger_type"][value="7"]', trumpetAttr('checked', (checked) => {
    if (checked === 'checked') {
      trumpetParser.nextPayload['_trigger_type'] = '7'
      // tab is selected, add more parsers
 
      // sensor ID
      trumpetParser.select(
        'form#scenarioForm select[name="_trigger[7]"] option[selected="selected"]',
        trumpetAffectValue(trumpetParser.nextPayload, '_trigger[7]')
      )
    }
  }))
 
  // Condition: Alarm level ✔
  trumpetParser.select('form#scenarioForm div.conditions span.input-checkbox.condition-choice.condition-3-choice[data-default="3"]', (element) => {
    trumpetParser.nextPayload['_condition_3'] = '3'
    trumpetParser.select(
      'form#scenarioForm div.conditions input[type="radio"][name^="_condition[3]"][checked="checked"]',
      trumpetAttr('name', (name) => {
        trumpetParser.nextPayload[name] = '1'
      })
    )
  })
 
  // Condition: time ✔
  trumpetParser.select('form#scenarioForm div.conditions span.input-checkbox.condition-choice.condition-1-choice[data-default="1"]', (element) => {
    trumpetParser.nextPayload['_condition_1'] = '1'
    trumpetParser.select(
      'form#scenarioForm div.conditions input[type="hidden"][name="_condition[1]"]',
      trumpetAffectValue(trumpetParser.nextPayload)
    )
  })
 
  // Condition: temperatures ✔
  trumpetParser.select('form#scenarioForm div.conditions span.input-checkbox.condition-choice.condition-Limited.4-1[data-default="4"]', (element) => {
    trumpetParser.nextPayload['_condition_4_1'] = '4'
    // checked, add more parsers
 
    trumpetParser.select(
      'form#scenarioForm div.conditions select[name="_condition[4][1][deviceSlaveId]"] option[selected="selected"]',
      trumpetAffectValue(trumpetParser.nextPayload, '_condition[4][1][deviceSlaveId]')
    )
    trumpetParser.select(
      'form#scenarioForm div.conditions select[name="_condition[4][1][operator]"] option[selected="selected"]',
      trumpetAffectValue(trumpetParser.nextPayload, '_condition[4][1][operator]')
    )
    trumpetParser.select(
      'form#scenarioForm div.conditions input[name="_condition[4][1][value]"]',
      trumpetAffectValue(trumpetParser.nextPayload)
    )
 
    trumpetParser.select('form#scenarioForm div.conditions span.input-checkbox.condition-choice.condition-Limited.4-2[data-default="4"]', (element) => {
      trumpetParser.nextPayload['_condition_4_2'] = '4'
      // checked, add more parsers
 
      trumpetParser.select(
        'form#scenarioForm div.conditions select[name="_condition[4][2][deviceSlaveId]"] option[selected="selected"]',
        trumpetAffectValue(trumpetParser.nextPayload, '_condition[4][2][deviceSlaveId]')
      )
      trumpetParser.select(
        'form#scenarioForm div.conditions select[name="_condition[4][2][operator]"] option[selected="selected"]',
        trumpetAffectValue(trumpetParser.nextPayload, '_condition[4][2][operator]')
      )
      trumpetParser.select(
        'form#scenarioForm div.conditions input[name="_condition[4][2][value]"]',
        trumpetAffectValue(trumpetParser.nextPayload)
      )
    })
  })
 
  // Condition: lightnings ✔
  trumpetParser.select('form#scenarioForm div.conditions span.input-checkbox.condition-choice.condition-Limited.5-1[data-default="5"]', (element) => {
    trumpetParser.nextPayload['_condition_5_1'] = '5'
    // checked, add more parsers
 
    trumpetParser.select(
      'form#scenarioForm div.conditions select[name="_condition[5][1][deviceSlaveId]"] option[selected="selected"]',
      trumpetAffectValue(trumpetParser.nextPayload, '_condition[5][1][deviceSlaveId]')
    )
    trumpetParser.select(
      'form#scenarioForm div.conditions select[name="_condition[5][1][operator]"] option[selected="selected"]',
      trumpetAffectValue(trumpetParser.nextPayload, '_condition[5][1][operator]')
    )
    trumpetParser.select(
      'form#scenarioForm div.conditions select[name="_condition[5][1][value]"] option[selected="selected"]',
      trumpetAffectValue(trumpetParser.nextPayload, '_condition[5][1][value]')
    )
 
    trumpetParser.select('form#scenarioForm div.conditions span.input-checkbox.condition-choice.condition-Limited.5-2[data-default="5"]', (element) => {
      trumpetParser.nextPayload['_condition_5_2'] = '5'
      // checked, add more parsers
 
      trumpetParser.select(
        'form#scenarioForm div.conditions select[name="_condition[5][2][deviceSlaveId]"] option[selected="selected"]',
        trumpetAffectValue(trumpetParser.nextPayload, '_condition[5][2][deviceSlaveId]')
      )
      trumpetParser.select(
        'form#scenarioForm div.conditions select[name="_condition[5][2][operator]"] option[selected="selected"]',
        trumpetAffectValue(trumpetParser.nextPayload, '_condition[5][2][operator]')
      )
      trumpetParser.select(
        'form#scenarioForm div.conditions select[name="_condition[5][2][value]"] option[selected="selected"]',
        trumpetAffectValue(trumpetParser.nextPayload, '_condition[5][2][value]')
      )
    })
  })
 
  return trumpetParser
}
 
/**
 * Returns a trumpet parser that will inspect form structure to post with no modification
 *
 * POST https://myfox.me/scenario/{homeId}/manage/{scenarioId}/4
 *    with all data obtained by this.step3TempModificationParser() in the payload
 * => <form id="scenarioForm" action="https://myfox.me/scenario/7718/manage/183349/5" method="post">
 *      <input type="hidden" name="scData" value="...">
 *      <h2>...</h2>
 *      <div class="tabs-contents" style="min-height: 209px;">
 *
 *        <div class="tab-content tab-security" style="display: block;">
 *          <div class="item security-level">
 *            <input type="radio" name="_action[securityLevel]" value="4" disabled="disabled">
 *            <input type="radio" name="_action[securityLevel]" value="2" disabled="disabled">
 *            <input type="radio" name="_action[securityLevel]" value="1" disabled="disabled">
 *          </div>
 *        </div>
 *
 *        <div class="tab-content tab-domotic">
 *          <input type="checkbox" name="_cbx161355" value="1" disabled="disabled">
 *          <input type="hidden" name="_action[g][2856]" value="" disabled="disabled">
 *          <input type="checkbox" name="_cbx825488" value="1" disabled="disabled">
 *          <input type="hidden" name="_action[d][71394]" value="" disabled="disabled">
 *        </div>
 *
 * Probleme, je n'ai pas d'exemple ici:
 *        <div class="tab-content tab-video">
 *          <div class="item">Vous n'avez aucune caméra installée.</div>
 *        </div>
 *        <div class="tab-content tab-automatism">
 *          <div class="item">Vous n'avez aucun automatisme installé.</div>
 *        </div>
 *
 *
 *        <div class="tab-content tab-scenario">
 *
 *          <!-- Jouer une sonnerie à l'exécution du scénario -->
 *          <input type="checkbox" name="ringtone" value="1" disabled="disabled">
 *
 *          <!-- Différer l'exécution du scénario -->
 *          <input type="checkbox" name="_cbx836372" value="1" disabled="disabled">
 *          de
 *          <input type="text" name="delayPlay" value="30" class="seconds" disabled="disabled">
 *          secondes
 *
 *          <!-- Lancer le scénario inverse -->
 *          <input type="checkbox" name="_cbx866445" value="1" disabled="disabled">
 *          après
 *          <input type="text" name="delayReverse" value="30" class="seconds" disabled="disabled">
 *          secondes
 *
 *          <!-- Lancer un autre scénario -->
 *          <input type="checkbox" name="_cbx294634" value="1" checked="checked">
 *          <select name="_action[runScenarioId]">
 *            <option value="">Choisissez un de vos scénarios à la demande</option>
 *            <option value="143997" selected="selected">AA test rien</option>
 *            <option value="39004">Ch Chbres Confort 2h</option>
 *          </select>
 *
 *        </div>
 *
 *      </div>
 *      <div class="form-buttons">
 *        <a href="#" class="btn-previousStep"><span class="arrow"></span><input type="submit" name="sbt" value="Étape précédente"><span class="text">Étape précédente</span></a>
 *        <a href="#" class="btn-nextStep"><input type="submit" name="sbt" value="Étape suivante"><span class="text">Étape suivante</span><span class="arrow"></span></a>
 *      </div>
 *    </form>
 *
 *
 * =======================================
 * Form elements are classified like this:
 * =======================================
 *
 * <input type="hidden" name="scData" value="...">
 *
 * Alarm levels:
 *   <input type="radio" name="_action[securityLevel]" value="4" disabled="disabled">
 *   <input type="radio" name="_action[securityLevel]" value="2" disabled="disabled">
 *   <input type="radio" name="_action[securityLevel]" value="1" disabled="disabled">
 *
 * Domotic/groups/other commands ON/OFF
 *   <div class="tab-content tab-domotic">
 *     <span class="input-checkbox action-choice" data-default="1">  <-- if data-default is == "1", then checked
 *       <input type="checkbox" name="_cbxXXXXX" value="1">  <-- don't care
 *     </span>
 *     <input type="hidden" name="_action[g][XXXXX]" value="">  <-- to take only if data-default == "1" above
 *
 *     <span class="input-checkbox action-choice" data-default="">  <-- example unchecked
 *       <input type="checkbox" name="_cbxXXXXX" value="1">  <-- don't care
 *     </span>
 *     <input type="hidden" name="_action[d][XXXXX]" value="">  <-- do not take it (unchecked)
 *   </div>
 *
 * Camera, automatism: PROBLEM, I HAVE NO SAMPLE !
 *   <div class="tab-content tab-video">
 *     <div class="item">Vous n'avez aucune caméra installée.</div>
 *   </div>
 *   <div class="tab-content tab-automatism">
 *     <div class="item">Vous n'avez aucun automatisme installé.</div>
 *   </div>
 * ==> To do, one day maybe...
 *
 * Ringtone:
 *   <span class="input-checkbox action-choice" data-default="1">  <-- if data-default is == "1", then checked
 *     <input type="checkbox" name="ringtone" value="1">  <-- don't care
 *   </span>
 *
 * Scenario play:
 *   <span class="input-checkbox action-choice" data-default="1">  <-- if data-default is == "1", then checked
 *     <input type="checkbox" name="_cbxXXXXX" value="1">  <-- don't care
 *   </span> de
 *   <input type="text" name="delayPlay" value="30" class="seconds" disabled="disabled">  <-- to take only if data-default == "1" above
 *
 *   <span class="input-checkbox action-choice" data-default="1">  <-- if data-default is == "1", then checked
 *     <input type="checkbox" name="_cbxXXXXX" value="1">  <-- don't care
 *   </span> après
 *   <input type="text" name="delayReverse" value="30" class="seconds" disabled="disabled">  <-- to take only if data-default == "1" above
 *
 *   <span class="input-checkbox action-choice" data-default="1">  <-- if data-default is == "1", then checked
 *     <input type="checkbox" name="_cbxXXXXX" value="1">  <-- don't care
 *   </span>
 *   <select name="_action[runScenarioId]">
 *     <option value="">Choisissez un de vos scénarios à la demande</option>
 *     <option value="xxxxx" selected="selected">AA test rien</option>
 *     <option value="xxxxxx">Ch Chbres Confort 2h</option>
 *   </select>
 *
 * @param  {MyfoxWrapperApiCommon}  wrapperApi  The Api instance, to throw events
 * @return {trumpet}                The trumpet parser to plug on the HTML stream.
 */
export function step4Parser (wrapperApi) {
  const trumpetParser = trumpet()
  trumpetParser.nextPayload = {}
  trumpetParser.setMaxListeners(10) // We have a lot of stuff to analyze...
 
  // get scData ✔
  trumpetParser.select(
    'form#scenarioForm input[name="scData"]',
    trumpetAffectValue(trumpetParser.nextPayload)
  )
 
  // Alarm levels
  trumpetParser.select(
    'form#scenarioForm input[type="radio"][name="_action[securityLevel]"][checked="checked"]',
    trumpetAffectValue(trumpetParser.nextPayload)
  )
 
  // Domotic/groups/other commands ON/OFF
  let nextInputForDomotic = false
  trumpetParser.selectAll(
    'form#scenarioForm div.tab-domotic span.input-checkbox.action-choice',
    trumpetAttr('data-default', (dataDefault) => {
      nextInputForDomotic = (dataDefault === '1')
    })
  )
  trumpetParser.selectAll(
    'form#scenarioForm div.tab-domotic input[type="hidden"][name^="_action"]',
    (element) => {
      element.getAttributes((attributes) => {
        if (nextInputForDomotic && attributes['value'] && attributes['value'] !== '') {
          trumpetParser.nextPayload[attributes['name']] = attributes['value']
        }
      })
    }
  )
 
  // Camera, automatism
  // To do later, when sample available!
 
  // Scenario play
  let nextInputForScenarioPlay = false
  trumpetParser.selectAll(
    'form#scenarioForm div.tab-scenario span.input-checkbox',
    trumpetAttr('data-default', (dataDefault) => {
      nextInputForScenarioPlay = (dataDefault === '1')
    })
  )
  trumpetParser.select(
    'form#scenarioForm div.tab-scenario input[type="checkbox"][name="ringtone"]',
    (element) => {
      element.getAttributes((attributes) => {
        Eif (nextInputForScenarioPlay) {
          trumpetParser.nextPayload['ringtone'] = true
        }
      })
    }
  )
  trumpetParser.select(
    'form#scenarioForm div.tab-scenario input[type="text"][name="delayPlay"]',
    (element) => {
      element.getAttributes((attributes) => {
        Eif (nextInputForScenarioPlay && attributes['value'] && attributes['value'] !== '') {
          trumpetParser.nextPayload['delayPlay'] = attributes['value']
        }
      })
    }
  )
  trumpetParser.select(
    'form#scenarioForm div.tab-scenario input[type="text"][name="delayReverse"]',
    (element) => {
      element.getAttributes((attributes) => {
        Iif (nextInputForScenarioPlay && attributes['value'] && attributes['value'] !== '') {
          trumpetParser.nextPayload['delayReverse'] = attributes['value']
        }
      })
    }
  )
  trumpetParser.select(
    'form#scenarioForm div.tab-scenario select[name="_action[runScenarioId]"] option[selected="selected"]',
    (element) => {
      element.getAttributes((attributes) => {
        Iif (nextInputForScenarioPlay && attributes['value'] && attributes['value'] !== '') {
          trumpetParser.nextPayload['_action[runScenarioId]'] = attributes['value']
        }
      })
    }
  )
 
  return trumpetParser
}
 
export function step5Parser (wrapperApi) {
  const trumpetParser = trumpet()
  trumpetParser.data = {status: 'ko'}
 
  trumpetParser.select('div.ended p.message-normal', () => {
    trumpetParser.data.status = 'ok'
    trumpetParser.end()
  })
 
  return trumpetParser
}
 
export function step3TempFixer (payload, settings) {
  // operators trigger_4: up_to:1, down_to:0
  // operators condition_4_*: <:0, >:1, =:2
  // DELTA to apply: operator 0 : -0.5°  ;  operator 1 : +0.5°  ;  operator 2 : +0°
  settings.forEach((setting) => {
    const {toTemperature, controls} = setting
    controls.forEach((control) => {
      let delta = 0
      switch (control.condition) {
        case 'trigger_4':
          if (payload['_trigger[4][1][deviceSlaveId]'] !== control.deviceSlaveId) {
            throw new Error('Scenario settings mismatch.')
          }
          delta = (payload['_trigger[4][1][operator]'] === '1') ? 0.5 : -0.5
          payload['_trigger[4][1][value]'] = Number(toTemperature) + delta + ''
          break
        case 'condition_4_1':
          Iif (payload['_condition[4][1][deviceSlaveId]'] !== control.deviceSlaveId) {
            throw new Error('Scenario settings mismatch.')
          }
          delta = (payload['_condition[4][1][operator]'] === '1')
              ? 0.5 : ((payload['_condition[4][1][operator]'] === '0') ? -0.5 : 0)
          payload['_condition[4][1][value]'] = Number(toTemperature) + delta + ''
          break
        case 'condition_4_2':
          Iif (payload['_condition[4][2][deviceSlaveId]'] !== control.deviceSlaveId) {
            throw new Error('Scenario settings mismatch.')
          }
          delta = (payload['_condition[4][2][operator]'] === '1')
              ? 0.5 : ((payload['_condition[4][2][operator]'] === '0') ? -0.5 : 0)
          payload['_condition[4][2][value]'] = Number(toTemperature) + delta + ''
          break
        default:
          throw new Error('Unknown condition to control.')
      }
    })
  })
  return payload
}