// File: MichaelXanth[P]RPG_Game/Market/Market.cpp
// Function: Market::Buy(std::vector<Hero*, std::allocator<Hero*>>&)
void Market::Buy(vector<Hero *>& heroes)
{
    unsigned int i;

    int action;
    string testAction;
    bool infoState     = false;
    Hero* selectedHero = NULL;

    if (heroes.size() == 1 && isAbleToBuy(heroes[0]) == -1) {
        usleep(2500000);
        DisplayMarket();
        return;
    
    } else {
        for (i = 0; i < heroes.size() && isAbleToBuy(heroes[i],false) == -1; i++)
            ;

        if (i == heroes.size()) {
            cerr << endl << "\033[31mError:\033[0m No one of the heroes are able to buy any Items or Spells from the Market.";
            usleep(2500000);
            DisplayMarket();
            return;
        }
    }

    while(true) {
        
        int buyState;

        if (heroes.size() == 1) {
            selectedHero = heroes[0];
        
        } else {
        
            for (i = 0; i < heroes.size() && isAbleToBuy(heroes[i],false) == -1; i++)
                ;

            if (i == heroes.size()) {
                cerr << endl << "-------------------------------" << endl << endl;
                cerr << "\033[35mWarning:\033[0m There are no other heroes able to buy any Items or Spells from the Market.";
                usleep(3000000);
                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 ((buyState = isAbleToBuy(selectedHero)) != -1) {
            bool errFlag = false;

            DisplayMarket(infoState,selectedHero,buyState);

            cout << endl;
            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:
                    Purchase(*selectedHero,buyState);
                    break;
                case 2:
                    infoState = !infoState;
                    // DisplayMarket(infoState,selectedHero,buyState);
                    break;
                case 3:
                    DisplayMarket();
                    return;
            }
        
        } else if (heroes.size() == 1) {
            usleep(2500000);
            DisplayMarket();
            return;
        }

        if (buyState == -1) {
            DisplayMarket();
            return;
        }
    //        selectedHero = NULL;
    }
}

// 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 0x37a05
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 0x31610
movl %eax, -0x8c(%rbp)
jmp 0x379f6
movl -0x8c(%rbp), %eax
cmpl $-0x1, %eax
sete %al
movb %al, -0x80(%rbp)
movb -0x80(%rbp), %al
testb $0x1, %al
jne 0x37a0e
jmp 0x37a5f
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x4390
jmp 0x37a1a
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 0x37a39
movl $0x1, -0x64(%rbp)
jmp 0x38293
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4160
jmp 0x382a5
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 0x37acf
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 0x31610
movl %eax, -0x9c(%rbp)
jmp 0x37abd
movl -0x9c(%rbp), %eax
cmpl $-0x1, %eax
sete %al
movb %al, -0x8d(%rbp)
movb -0x8d(%rbp), %al
testb $0x1, %al
jne 0x37adb
jmp 0x37aeb
jmp 0x37add
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x37a66
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 0x37b75
movq 0x164dc(%rip), %rdi     # 0x4dff0
movq 0x1649d(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xb0(%rbp)
jmp 0x37b29
movq -0xb0(%rbp), %rdi
leaq 0x8c0c(%rip), %rsi      # 0x40743
callq 0x4260
jmp 0x37b3e
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x4390
jmp 0x37b4a
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 0x37b69
movl $0x1, -0x64(%rbp)
jmp 0x38293
jmp 0x37b77
jmp 0x37b79
jmp 0x37b7b
movq -0x10(%rbp), %rdi
callq 0x5ad0
cmpq $0x1, %rax
jne 0x37ba3
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5030
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x37fea
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 0x37c13
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 0x31610
movl %eax, -0xc4(%rbp)
jmp 0x37c01
movl -0xc4(%rbp), %eax
cmpl $-0x1, %eax
sete %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x37c1f
jmp 0x37c2f
jmp 0x37c21
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x37baa
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 0x37d0a
movq 0x16394(%rip), %rdi     # 0x4dff0
movq 0x16355(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xd8(%rbp)
jmp 0x37c71
movq -0xd8(%rbp), %rdi
leaq 0x8613(%rip), %rsi      # 0x40292
callq 0x4260
movq %rax, -0xe0(%rbp)
jmp 0x37c8d
movq -0xe0(%rbp), %rdi
movq 0x1631d(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xe8(%rbp)
jmp 0x37ca9
movq -0xe8(%rbp), %rdi
movq 0x16301(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x37cbe
movq 0x1632b(%rip), %rdi     # 0x4dff0
leaq 0x8ad1(%rip), %rsi      # 0x4079d
callq 0x4260
jmp 0x37cd3
movl $0x2dc6c0, %edi         # imm = 0x2DC6C0
callq 0x4390
jmp 0x37cdf
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 0x37cfe
movl $0x1, -0x64(%rbp)
jmp 0x38293
jmp 0x37d0c
cmpq $0x0, -0x48(%rbp)
jne 0x37fe8
movq 0x162aa(%rip), %rdi     # 0x4dfc8
movq 0x16293(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x37d2c
movq 0x16295(%rip), %rdi     # 0x4dfc8
leaq 0x8558(%rip), %rsi      # 0x40292
callq 0x4260
movq %rax, -0xf0(%rbp)
jmp 0x37d48
movq -0xf0(%rbp), %rdi
movq 0x16262(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0xf8(%rbp)
jmp 0x37d64
movq -0xf8(%rbp), %rdi
movq 0x16246(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x37d79
movq 0x16248(%rip), %rdi     # 0x4dfc8
leaq 0x8656(%rip), %rsi      # 0x403dd
callq 0x4260
movq %rax, -0x100(%rbp)
jmp 0x37d95
movq -0x100(%rbp), %rdi
movq 0x16215(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x37daa
movq 0x16217(%rip), %rdi     # 0x4dfc8
leaq 0x8a42(%rip), %rsi      # 0x407fa
callq 0x4260
movq %rax, -0x108(%rbp)
jmp 0x37dc6
movq -0x10(%rbp), %rdi
callq 0x5ad0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x4080
movq %rax, -0x110(%rbp)
jmp 0x37de7
movq -0x110(%rbp), %rdi
leaq 0x866c(%rip), %rsi      # 0x40461
callq 0x4260
jmp 0x37dfc
movq 0x161c5(%rip), %rdi     # 0x4dfc8
leaq 0x865e(%rip), %rsi      # 0x40468
callq 0x4260
jmp 0x37e11
movq 0x161b0(%rip), %rdi     # 0x4dfc8
leaq 0x89ef(%rip), %rsi      # 0x4080e
callq 0x4260
jmp 0x37e26
jmp 0x37e28
jmp 0x37e2a
movb $0x0, -0x6f(%rbp)
movq 0x16193(%rip), %rdi     # 0x4dfc8
leaq 0x5a42(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x37e43
jmp 0x37e45
testb $0x1, -0x6f(%rbp)
je 0x37e59
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x4390
jmp 0x37e57
jmp 0x37e59
movq 0x16168(%rip), %rdi     # 0x4dfc8
leaq 0x5a41(%rip), %rsi      # 0x3d8a8
callq 0x4260
jmp 0x37e6e
movq 0x16153(%rip), %rdi     # 0x4dfc8
leaq 0x79b7(%rip), %rsi      # 0x3f833
callq 0x4260
jmp 0x37e83
movq 0x1613e(%rip), %rdi     # 0x4dfc8
leaq 0x59ed(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x37e98
movq 0x16139(%rip), %rdi     # 0x4dfd8
leaq -0x38(%rbp), %rsi
callq 0x4340
jmp 0x37eaa
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 0x37ed0
movb -0x111(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x37e45
cmpl $0x0, -0x18(%rbp)
jl 0x37f0d
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 0x37f92
movq 0x160dc(%rip), %rdi     # 0x4dff0
movq 0x1609d(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0x128(%rbp)
jmp 0x37f29
movq -0x128(%rbp), %rdi
leaq 0x88e1(%rip), %rsi      # 0x40818
callq 0x4260
movq %rax, -0x130(%rbp)
jmp 0x37f45
movq -0x130(%rbp), %rdi
movq 0x16065(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x37f5a
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x4390
jmp 0x37f66
movq 0x1605b(%rip), %rdi     # 0x4dfc8
leaq 0x5934(%rip), %rsi      # 0x3d8a8
callq 0x4260
jmp 0x37f7b
movq 0x16046(%rip), %rdi     # 0x4dfc8
leaq 0x78aa(%rip), %rsi      # 0x3f833
callq 0x4260
jmp 0x37f90
jmp 0x37fde
cmpl $0x0, -0x18(%rbp)
jne 0x37fc3
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 0x37fb7
movl $0x1, -0x64(%rbp)
jmp 0x38293
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x5030
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x37fe3
jmp 0x37e28
jmp 0x37d0c
jmp 0x37fea
movq -0x88(%rbp), %rdi
movq -0x48(%rbp), %rsi
movb $0x1, -0x71(%rbp)
leaq -0x71(%rbp), %rdx
callq 0x31610
movl %eax, -0x134(%rbp)
jmp 0x3800a
movl -0x134(%rbp), %eax
movl %eax, -0x6c(%rbp)
cmpl $-0x1, %eax
je 0x3821b
movq -0x88(%rbp), %rdi
movb $0x0, -0x72(%rbp)
movq -0x48(%rbp), %rdx
movl -0x6c(%rbp), %ecx
leaq -0x39(%rbp), %rsi
callq 0x32490
jmp 0x38039
movq 0x15f88(%rip), %rdi     # 0x4dfc8
movq 0x15f71(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x3804e
movq 0x15f73(%rip), %rdi     # 0x4dfc8
leaq 0x8236(%rip), %rsi      # 0x40292
callq 0x4260
movq %rax, -0x140(%rbp)
jmp 0x3806a
movq -0x140(%rbp), %rdi
movq 0x15f40(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0x148(%rbp)
jmp 0x38086
movq -0x148(%rbp), %rdi
movq 0x15f24(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x3809b
movq 0x15f26(%rip), %rdi     # 0x4dfc8
leaq 0x87aa(%rip), %rsi      # 0x40853
callq 0x4260
jmp 0x380b0
movq 0x15f11(%rip), %rdi     # 0x4dfc8
leaq 0x83aa(%rip), %rsi      # 0x40468
callq 0x4260
jmp 0x380c5
movq 0x15efc(%rip), %rdi     # 0x4dfc8
leaq 0x8793(%rip), %rsi      # 0x40866
callq 0x4260
jmp 0x380da
movq 0x15ee7(%rip), %rdi     # 0x4dfc8
leaq 0x5796(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x380ef
jmp 0x380f1
testb $0x1, -0x72(%rbp)
je 0x38105
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x4390
jmp 0x38103
jmp 0x38105
movq 0x15ebc(%rip), %rdi     # 0x4dfc8
leaq 0x5795(%rip), %rsi      # 0x3d8a8
callq 0x4260
jmp 0x3811a
movq 0x15ea7(%rip), %rdi     # 0x4dfc8
leaq 0x770b(%rip), %rsi      # 0x3f833
callq 0x4260
jmp 0x3812f
movq 0x15e92(%rip), %rdi     # 0x4dfc8
leaq 0x5741(%rip), %rsi      # 0x3d87e
callq 0x4260
jmp 0x38144
movq 0x15e8d(%rip), %rdi     # 0x4dfd8
leaq -0x38(%rbp), %rsi
callq 0x4340
jmp 0x38156
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 0x38193
movb -0x149(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x380f1
movl -0x18(%rbp), %eax
movl %eax, -0x150(%rbp)
subl $0x1, %eax
je 0x381cd
jmp 0x381b3
movl -0x150(%rbp), %eax
subl $0x2, %eax
je 0x381e5
jmp 0x381c0
movl -0x150(%rbp), %eax
subl $0x3, %eax
je 0x381f1
jmp 0x38219
movq -0x88(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq -0x6c(%rbp), %rdx
callq 0x364d0
jmp 0x381e3
jmp 0x38219
movb -0x39(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x38219
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 0x38210
movl $0x1, -0x64(%rbp)
jmp 0x38293
jmp 0x38260
movq -0x10(%rbp), %rdi
callq 0x5ad0
cmpq $0x1, %rax
jne 0x3825e
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x4390
jmp 0x38236
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 0x38255
movl $0x1, -0x64(%rbp)
jmp 0x38293
jmp 0x38260
cmpl $-0x1, -0x6c(%rbp)
jne 0x3828e
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 0x38285
movl $0x1, -0x64(%rbp)
jmp 0x38293
jmp 0x37b79
leaq -0x38(%rbp), %rdi
callq 0x4160
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x4440
nop