// File: 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
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4380
movb $0x0, -0x39(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x1, %rcx
movb %al, -0x80(%rbp)
jne 0x39535
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5030
movq -0x88(%rbp), %rdi
movq (%rax), %rsi
movb $0x1, -0x49(%rbp)
leaq -0x49(%rbp), %rdx
callq 0x382b0
movl %eax, -0x8c(%rbp)
jmp 0x39526
movl -0x8c(%rbp), %eax
cmpl $-0x1, %eax
sete %al
movb %al, -0x80(%rbp)
movb -0x80(%rbp), %al
testb $0x1, %al
jne 0x3953e
jmp 0x3958f
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x4390
jmp 0x3954a
movq -0x88(%rbp), %rdi
movb $0x0, -0x5d(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x5d(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x32490
jmp 0x39569
movl $0x1, -0x64(%rbp)
jmp 0x39dbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4160
jmp 0x39dcf
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq -0x98(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x8d(%rbp)
jae 0x395ff
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x5030
movq -0x88(%rbp), %rdi
movq (%rax), %rsi
movb $0x0, -0x65(%rbp)
leaq -0x65(%rbp), %rdx
callq 0x382b0
movl %eax, -0x9c(%rbp)
jmp 0x395ed
movl -0x9c(%rbp), %eax
cmpl $-0x1, %eax
sete %al
movb %al, -0x8d(%rbp)
movb -0x8d(%rbp), %al
testb $0x1, %al
jne 0x3960b
jmp 0x3961b
jmp 0x3960d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x39596
movl -0x14(%rbp), %eax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
jne 0x396a5
movq 0x149ac(%rip), %rdi     # 0x4dff0
movq 0x1496d(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xb0(%rbp)
jmp 0x39659
movq -0xb0(%rbp), %rdi
leaq 0x737d(%rip), %rsi      # 0x409e4
callq 0x4260
jmp 0x3966e
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x4390
jmp 0x3967a
movq -0x88(%rbp), %rdi
movb $0x0, -0x66(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x66(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x32490
jmp 0x39699
movl $0x1, -0x64(%rbp)
jmp 0x39dbd
jmp 0x396a7
jmp 0x396a9
jmp 0x396ab
movq -0x10(%rbp), %rdi
callq 0x5ad0
cmpq $0x1, %rax
jne 0x396d3
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5030
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x39b1a
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq -0xc0(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0xb1(%rbp)
jae 0x39743
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x5030
movq -0x88(%rbp), %rdi
movq (%rax), %rsi
movb $0x0, -0x6d(%rbp)
leaq -0x6d(%rbp), %rdx
callq 0x382b0
movl %eax, -0xc4(%rbp)
jmp 0x39731
movl -0xc4(%rbp), %eax
cmpl $-0x1, %eax
sete %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x3974f
jmp 0x3975f
jmp 0x39751
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x396da
movl -0x14(%rbp), %eax
movq %rax, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jne 0x3983a
movq 0x14864(%rip), %rdi     # 0x4dff0
movq 0x14825(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xd8(%rbp)
jmp 0x397a1
movq -0xd8(%rbp), %rdi
leaq 0x6ae3(%rip), %rsi      # 0x40292
callq 0x4260
movq %rax, -0xe0(%rbp)
jmp 0x397bd
movq -0xe0(%rbp), %rdi
movq 0x147ed(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xe8(%rbp)
jmp 0x397d9
movq -0xe8(%rbp), %rdi
movq 0x147d1(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x397ee
movq 0x147fb(%rip), %rdi     # 0x4dff0
leaq 0x7230(%rip), %rsi      # 0x40a2c
callq 0x4260
jmp 0x39803
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x4390
jmp 0x3980f
movq -0x88(%rbp), %rdi
movb $0x0, -0x6e(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x6e(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x32490
jmp 0x3982e
movl $0x1, -0x64(%rbp)
jmp 0x39dbd
jmp 0x3983c
cmpq $0x0, -0x48(%rbp)
jne 0x39b18
movq 0x1477a(%rip), %rdi     # 0x4dfc8
movq 0x14763(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x3985c
movq 0x14765(%rip), %rdi     # 0x4dfc8
leaq 0x6a28(%rip), %rsi      # 0x40292
callq 0x4260
movq %rax, -0xf0(%rbp)
jmp 0x39878
movq -0xf0(%rbp), %rdi
movq 0x14732(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xf8(%rbp)
jmp 0x39894
movq -0xf8(%rbp), %rdi
movq 0x14716(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x398a9
movq 0x14718(%rip), %rdi     # 0x4dfc8
leaq 0x6b26(%rip), %rsi      # 0x403dd
callq 0x4260
movq %rax, -0x100(%rbp)
jmp 0x398c5
movq -0x100(%rbp), %rdi
movq 0x146e5(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x398da
movq 0x146e7(%rip), %rdi     # 0x4dfc8
leaq 0x6f12(%rip), %rsi      # 0x407fa
callq 0x4260
movq %rax, -0x108(%rbp)
jmp 0x398f6
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x4080
movq %rax, -0x110(%rbp)
jmp 0x39917
movq -0x110(%rbp), %rdi
leaq 0x6b3c(%rip), %rsi      # 0x40461
callq 0x4260
jmp 0x3992c
movq 0x14695(%rip), %rdi     # 0x4dfc8
leaq 0x6b2e(%rip), %rsi      # 0x40468
callq 0x4260
jmp 0x39941
movq 0x14680(%rip), %rdi     # 0x4dfc8
leaq 0x6ebf(%rip), %rsi      # 0x4080e
callq 0x4260
jmp 0x39956
jmp 0x39958
jmp 0x3995a
movb $0x0, -0x6f(%rbp)
movq 0x14663(%rip), %rdi     # 0x4dfc8
leaq 0x3f12(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x39973
jmp 0x39975
testb $0x1, -0x6f(%rbp)
je 0x39989
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x4390
jmp 0x39987
jmp 0x39989
movq 0x14638(%rip), %rdi     # 0x4dfc8
leaq 0x3f11(%rip), %rsi      # 0x3d8a8
callq 0x4260
jmp 0x3999e
movq 0x14623(%rip), %rdi     # 0x4dfc8
leaq 0x5e87(%rip), %rsi      # 0x3f833
callq 0x4260
jmp 0x399b3
movq 0x1460e(%rip), %rdi     # 0x4dfc8
leaq 0x3ebd(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x399c8
movq 0x14609(%rip), %rdi     # 0x4dfd8
leaq -0x38(%rbp), %rsi
callq 0x4340
jmp 0x399da
movb $0x1, -0x6f(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4100
movq %rax, %rdi
leaq -0x18(%rbp), %rsi
movl $0x1, %edx
callq 0x1e240
movb %al, -0x111(%rbp)
jmp 0x39a00
movb -0x111(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x39975
cmpl $0x0, -0x18(%rbp)
jl 0x39a3d
movslq -0x18(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq %rax, %rcx
movq -0x120(%rbp), %rax
cmpq %rcx, %rax
jbe 0x39ac2
movq 0x145ac(%rip), %rdi     # 0x4dff0
movq 0x1456d(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0x128(%rbp)
jmp 0x39a59
movq -0x128(%rbp), %rdi
leaq 0x6db1(%rip), %rsi      # 0x40818
callq 0x4260
movq %rax, -0x130(%rbp)
jmp 0x39a75
movq -0x130(%rbp), %rdi
movq 0x14535(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x39a8a
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x4390
jmp 0x39a96
movq 0x1452b(%rip), %rdi     # 0x4dfc8
leaq 0x3e04(%rip), %rsi      # 0x3d8a8
callq 0x4260
jmp 0x39aab
movq 0x14516(%rip), %rdi     # 0x4dfc8
leaq 0x5d7a(%rip), %rsi      # 0x3f833
callq 0x4260
jmp 0x39ac0
jmp 0x39b0e
cmpl $0x0, -0x18(%rbp)
jne 0x39af3
movq -0x88(%rbp), %rdi
movb $0x0, -0x70(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x70(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x32490
jmp 0x39ae7
movl $0x1, -0x64(%rbp)
jmp 0x39dbd
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x5030
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x39b13
jmp 0x39958
jmp 0x3983c
jmp 0x39b1a
movq -0x88(%rbp), %rdi
movq -0x48(%rbp), %rsi
movb $0x1, -0x71(%rbp)
leaq -0x71(%rbp), %rdx
callq 0x382b0
movl %eax, -0x134(%rbp)
jmp 0x39b3a
movl -0x134(%rbp), %eax
movl %eax, -0x6c(%rbp)
cmpl $-0x1, %eax
je 0x39d45
movb $0x0, -0x72(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x39(%rbp), %rsi
leaq -0x6c(%rbp), %rdx
callq 0x20b70
jmp 0x39b63
movq 0x1445e(%rip), %rdi     # 0x4dfc8
movq 0x14447(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x39b78
movq 0x14449(%rip), %rdi     # 0x4dfc8
leaq 0x670c(%rip), %rsi      # 0x40292
callq 0x4260
movq %rax, -0x140(%rbp)
jmp 0x39b94
movq -0x140(%rbp), %rdi
movq 0x14416(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0x148(%rbp)
jmp 0x39bb0
movq -0x148(%rbp), %rdi
movq 0x143fa(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x39bc5
movq 0x143fc(%rip), %rdi     # 0x4dfc8
leaq 0x6c80(%rip), %rsi      # 0x40853
callq 0x4260
jmp 0x39bda
movq 0x143e7(%rip), %rdi     # 0x4dfc8
leaq 0x6880(%rip), %rsi      # 0x40468
callq 0x4260
jmp 0x39bef
movq 0x143d2(%rip), %rdi     # 0x4dfc8
leaq 0x6c69(%rip), %rsi      # 0x40866
callq 0x4260
jmp 0x39c04
movq 0x143bd(%rip), %rdi     # 0x4dfc8
leaq 0x3c6c(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x39c19
jmp 0x39c1b
testb $0x1, -0x72(%rbp)
je 0x39c2f
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x4390
jmp 0x39c2d
jmp 0x39c2f
movq 0x14392(%rip), %rdi     # 0x4dfc8
leaq 0x3c6b(%rip), %rsi      # 0x3d8a8
callq 0x4260
jmp 0x39c44
movq 0x1437d(%rip), %rdi     # 0x4dfc8
leaq 0x5be1(%rip), %rsi      # 0x3f833
callq 0x4260
jmp 0x39c59
movq 0x14368(%rip), %rdi     # 0x4dfc8
leaq 0x3c17(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x39c6e
movq 0x14363(%rip), %rdi     # 0x4dfd8
leaq -0x38(%rbp), %rsi
callq 0x4340
jmp 0x39c80
movb $0x1, -0x72(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4100
movq %rax, %rdi
movl $0x1, -0x78(%rbp)
movl $0x3, -0x7c(%rbp)
leaq -0x18(%rbp), %rsi
leaq -0x78(%rbp), %rdx
leaq -0x7c(%rbp), %rcx
movl $0x1, %r8d
callq 0x1e340
movb %al, -0x149(%rbp)
jmp 0x39cbd
movb -0x149(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x39c1b
movl -0x18(%rbp), %eax
movl %eax, -0x150(%rbp)
subl $0x1, %eax
je 0x39cf7
jmp 0x39cdd
movl -0x150(%rbp), %eax
subl $0x2, %eax
je 0x39d0f
jmp 0x39cea
movl -0x150(%rbp), %eax
subl $0x3, %eax
je 0x39d1b
jmp 0x39d43
movq -0x88(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq -0x6c(%rbp), %rdx
callq 0x38420
jmp 0x39d0d
jmp 0x39d43
movb -0x39(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x39d43
movq -0x88(%rbp), %rdi
movb $0x0, -0x7d(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x7d(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x32490
jmp 0x39d3a
movl $0x1, -0x64(%rbp)
jmp 0x39dbd
jmp 0x39d8a
movq -0x10(%rbp), %rdi
callq 0x5ad0
cmpq $0x1, %rax
jne 0x39d88
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x4390
jmp 0x39d60
movq -0x88(%rbp), %rdi
movb $0x0, -0x7e(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x7e(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x32490
jmp 0x39d7f
movl $0x1, -0x64(%rbp)
jmp 0x39dbd
jmp 0x39d8a
cmpl $-0x1, -0x6c(%rbp)
jne 0x39db8
movq -0x88(%rbp), %rdi
movb $0x0, -0x7f(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x7f(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x32490
jmp 0x39daf
movl $0x1, -0x64(%rbp)
jmp 0x39dbd
jmp 0x396a9
leaq -0x38(%rbp), %rdi
callq 0x4160
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x4440
nopl (%rax,%rax)