// File: /workspace/github_cpp_makefile_star2_O2/MichaelXanth[P]RPG_Game/Market/Market.cpp
// Function: Market::Sell(std::vector<Hero*, std::allocator<Hero*>>&)
void Market::Sell(vector<Hero *>& heroes)
{
    unsigned int i;

    int action;
    string testAction;
    bool infoState     = false;
    Hero* selectedHero = NULL;

    if (heroes.size() == 1 && isAbleToSell(heroes[0]) == -1) {
        usleep(2500000);
        DisplayMarket();
        return;
    
    } else {
        for (i = 0; i < heroes.size() && isAbleToSell(heroes[i],false) == -1; i++)
            ;

        if (i == heroes.size()) {
            cerr << endl << "\033[31mError:\033[0m No one of the heroes have any Items or Spells for sale.";
            usleep(2500000);
            DisplayMarket();
            return;
        }
    }

    while(true) {
        int sellState;

        if (heroes.size() == 1) {
            selectedHero = heroes[0];
        
        } else {
            for (i = 0; i < heroes.size() && isAbleToSell(heroes[i],false) == -1; i++)
                ;

            if (i == heroes.size()) {
                cerr << endl << "-------------------------------" << endl << endl;
                cerr << "\033[35mWarning:\033[0m No one of the heroes have any Items or Spells left for sale.";
                usleep(2500000);
                DisplayMarket();
                return;
            }

            while(selectedHero == NULL) {
                //bool errorFlag = false;
                cout << endl;
                cout << "-------------------------------" << endl << endl;
                cout << "(Type 0 to cancel)" << endl;
                cout << "Choose a Hero (1 - " << heroes.size() << "): ";

                cout << "\n\n\n\n\n";
                cout << "\033[5A\033[23C";

                while(true) {
                    bool errFlag = false;

                    cout << "\033[s";

                    do {
                        if (errFlag)
                            usleep(1800000);

                        cout << "\033[u";
                        cout << "\033[J";
                        cout << "\033[s";

                        cin >> testAction;

                        errFlag = true;
                    } while(!isNumeric(testAction.c_str(),action));

                    if (action < 0 || action > heroes.size()) {
                        cerr << endl << "\033[31mError:\033[0m Please choose a number in the given range." << endl;


                        usleep(1800000);

                        cout << "\033[u";
                        cout << "\033[J";

                        //cout << "\033[K";
                        //cout << "\033[1A\033[K";
                        //cout << "\033[1A\033[K";

                        //if (errorFlag)
                        //    cout << "\033[1A\033[K";

                        //errorFlag = true;
                    
                    } else {
                        if (!action) {
                            DisplayMarket();
                            return;
                        }
                        selectedHero = heroes[action - 1];
                        break;
                    }
                }
            }
        }

        if ((sellState = isAbleToSell(selectedHero)) != -1) {
            bool errFlag = false;

            selectedHero->DisplayProdsForSale(infoState,sellState);

            cout << endl;
            /*if (sellState == 0 || sellState == 1)
            {
                cout << "\033[44C-------------------------------" << endl << endl;
                cout << "\033[44CChoose an action: ";
            
            } else {*/
                cout << "-------------------------------" << endl << endl;
                cout << "Choose an action: ";
            //}

            cout << "\n\n\n\n\n";
            cout << "\033[5A\033[18C";

            cout << "\033[s";

            do {
                if (errFlag)
                    usleep(1800000);

                cout << "\033[u";
                cout << "\033[J";
                cout << "\033[s";

                cin >> testAction;

                errFlag = true;
            } while(!isValid(testAction.c_str(),action,1,3));

            switch(action)
            {
                case 1:
                    Sale(*selectedHero,sellState);
                    break;
                case 2:
                    infoState = !infoState;
                    //selectedHero->DisplayProdsForSale(infoState,sellState);
                    break;
                case 3:
                    DisplayMarket();
                    return;
            }
        
        } else if (heroes.size() == 1) {
            usleep(2500000);
            DisplayMarket();
            return;
        }

        if (sellState == -1) {
            DisplayMarket();
            return;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, -0x48(%rbp)
xorl %ecx, %ecx
movb %cl, (%rax)
movb %cl, -0x29(%rbp)
movq (%rsi), %rax
movq %rsi, -0x40(%rbp)
movq 0x8(%rsi), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x18c2d
movq (%rax), %rsi
leaq -0x34(%rbp), %rdx
movb $0x1, (%rdx)
callq 0x181d4
cmpl $-0x1, %eax
je 0x1902f
xorl %ecx, %ecx
leaq -0x34(%rbp), %rbx
movl %ecx, %r14d
movq -0x40(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x18c7e
movq (%rax,%r14,8), %rsi
movb $0x0, -0x34(%rbp)
movq %rbx, %rdx
callq 0x181d4
leal 0x1(%r14), %ecx
cmpl $-0x1, %eax
je 0x18c33
movq -0x40(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jne 0x18cc2
movq 0xe366(%rip), %rdi      # 0x26ff0
callq 0x30d0
leaq 0x5d0e(%rip), %rsi      # 0x1e9a4
movq %rax, %rdi
callq 0x31d0
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x32a0
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x1904e
movq $0x0, -0x58(%rbp)
movq 0xe2f7(%rip), %r12      # 0x26fc8
leaq 0x2ba6(%rip), %r13      # 0x1b87e
leaq 0x2bc9(%rip), %rbx      # 0x1b8a8
leaq 0x4abd(%rip), %r15      # 0x1d7a3
pushq $0x1
popq %r14
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x18df8
movq (%rax), %rsi
movb $0x1, -0x30(%rbp)
movq %rsi, -0x58(%rbp)
leaq -0x30(%rbp), %rdx
callq 0x181d4
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x18fd9
movq -0x58(%rbp), %rdi
leaq -0x29(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0xdb5e
movq %r12, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x5518(%rip), %rsi      # 0x1e252
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x5aba(%rip), %rsi      # 0x1e813
callq 0x31d0
movq %r12, %rdi
leaq 0x56c0(%rip), %rsi      # 0x1e428
callq 0x31d0
movq %r12, %rdi
leaq 0x5aaf(%rip), %rsi      # 0x1e826
callq 0x31d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x31d0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x31d0
movq %r12, %rdi
movq %r15, %rsi
callq 0x31d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x31d0
movq 0xe229(%rip), %rdi      # 0x26fd8
leaq -0x88(%rbp), %rsi
callq 0x3250
movq -0x88(%rbp), %rdi
movl $0x1, -0x30(%rbp)
movl $0x3, -0x5c(%rbp)
leaq -0x4c(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x5c(%rbp), %rcx
movl %r14d, %r8d
callq 0xcb5c
testb %al, %al
jne 0x18fc2
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x32a0
jmp 0x18d87
xorl %ecx, %ecx
movl %ecx, %edx
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %rdx, %rax
jbe 0x18e43
movq (%rcx,%rdx,8), %rsi
movb $0x0, -0x30(%rbp)
movq %rdx, -0x68(%rbp)
leaq -0x30(%rbp), %rdx
callq 0x181d4
movq -0x68(%rbp), %rdx
leal 0x1(%rdx), %ecx
cmpl $-0x1, %eax
je 0x18dfa
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
cmpq %rdx, %rax
movq -0x58(%rbp), %rsi
je 0x19080
testq %rsi, %rsi
jne 0x18cfa
movq %r12, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x53e7(%rip), %rsi      # 0x1e252
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x5513(%rip), %rsi      # 0x1e39d
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x5919(%rip), %rsi      # 0x1e7ba
callq 0x31d0
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rsi
subq (%rcx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x555a(%rip), %rsi      # 0x1e421
callq 0x31d0
movq %r12, %rdi
leaq 0x5552(%rip), %rsi      # 0x1e428
callq 0x31d0
movq %r12, %rdi
leaq 0x58e9(%rip), %rsi      # 0x1e7ce
callq 0x31d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x31d0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x31d0
movq %r12, %rdi
movq %r15, %rsi
callq 0x31d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x31d0
movq 0xe0bb(%rip), %rdi      # 0x26fd8
leaq -0x88(%rbp), %rsi
callq 0x3250
movq -0x88(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl %r14d, %edx
callq 0xcac0
testb %al, %al
jne 0x18f4c
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x32a0
jmp 0x18ef5
movslq -0x4c(%rbp), %rax
testq %rax, %rax
js 0x18f6c
movq -0x40(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jae 0x18fb4
movq 0xe07d(%rip), %rdi      # 0x26ff0
callq 0x30d0
movq %rax, %rdi
leaq 0x5856(%rip), %rsi      # 0x1e7d8
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x32a0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x31d0
movq %r12, %rdi
movq %r15, %rsi
callq 0x31d0
jmp 0x18eea
testl %eax, %eax
je 0x19018
movq -0x8(%rcx,%rax,8), %rsi
jmp 0x18e50
movl -0x4c(%rbp), %eax
cmpl $0x1, %eax
je 0x18ff5
cmpl $0x2, %eax
je 0x18fef
cmpl $0x3, %eax
jne 0x19002
jmp 0x19106
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cmpq $0x8, %rax
jne 0x19002
jmp 0x190e2
xorb $0x1, -0x29(%rbp)
jmp 0x19002
movq -0x58(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x182bc
cmpl $-0x1, -0x34(%rbp)
je 0x19069
movq -0x40(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
jmp 0x18cea
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x1904e
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x32a0
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
leaq -0x88(%rbp), %rdi
callq 0x30c0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x1904e
movq 0xdf69(%rip), %rdi      # 0x26ff0
callq 0x30d0
leaq 0x51bf(%rip), %rsi      # 0x1e252
movq %rax, %rdi
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq 0xdf3e(%rip), %rdi      # 0x26ff0
leaq 0x5933(%rip), %rsi      # 0x1e9ec
callq 0x31d0
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x32a0
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x1904e
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x32a0
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x1904e
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x48(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x1904e
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
jmp 0x19144
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x30c0
movq %rbx, %rdi
callq 0x3330
nop