import sys
import random
from matplotlib import pyplot
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


# GUI to implement later
class Dialog(QDialog):
    NumGridRows = 3
    NumButtons = 4

    def __init__(self):
        super(Dialog, self).__init__()
        self.formGroupBox = QGroupBox("Character Info")
        self.classBox = QComboBox()
        layout = QFormLayout()
        # layout.addRow(QLabel("Name:"), QLineEdit())
        layout.addRow(QLabel("Class:"), self.classBox)
        # layout.addRow(QLabel("Age:"), QSpinBox())
        self.formGroupBox.setLayout(layout)
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.formGroupBox)
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)
        self.setWindowTitle("Weapon Config")


class Weapon:
    def __init__(self, name, atkbonus, dmgdie, dmgbonus, atkct):
        self.name = name  # weapon name
        self.atkBonus = atkbonus  # bonus to add to attack die rolls
        self.dmgDie = dmgdie  # damage die to roll
        self.dmgBonus = dmgbonus  # bonus to add to damage die rolls
        self.atkCt = atkct  # number of attacks per turn
        self.avgs = []
        self.totalDmg = 0

    def attack(self):
        return random.randrange(1, 21, 1) + self.atkBonus

    def dmg(self):
        return random.randrange(1, self.dmgDie + 1, 1) + self.dmgBonus


random.seed()

# begin user-defined section
ac_min = 1
ac_max = 30
crit_roll = 19
extra_attack = True
weapons = [
           # Weapon("Heavy Crossbow, no feats", 8, 10, 4, 1),  # level 3
           Weapon("Heavy Crossbow, DEX ASI", 9, 10, 5, 1),  # level 4
           Weapon("Heavy Crossbow, sharpshooter", 3, 10, 14, 1),  # level 4
           Weapon("Hand Crossbow, crossbow expert", 8, 6, 4, 3),  # level 4
           Weapon("Longbow, DEX ASI", 9, 8, 5, 2),  # level 4
           # Weapon("Heavy Crossbow, DEX ASI", 10, 10, 5, 1),  # level 5
           # Weapon("Heavy Crossbow, sharpshooter", 4, 10, 14, 1),  # level 5
           # Weapon("Hand Crossbow, crossbow expert", 9, 6, 4, 4),  # level 5
           # Weapon("Longbow, DEX ASI", 9, 8, 5, 3),  # level 5
           # Weapon("Heavy Crossbow, DEX ASI+sharpshooter", 5, 10, 15, 1),  # level 6
           # Weapon("Hand Crossbow, DEX ASI+crossbow expert", 10, 6, 5, 3),  # level 6
           # Weapon("Hand Crossbow, sharpshooter+crossbow expert", 4, 6, 14, 3),  # level 6
           # Weapon("Longbow, DEX ASI+sharpshooter", 5, 8, 15, 2),  # level 6
           ]
# end user-defined section

app = QApplication(sys.argv)
dialog = Dialog()
colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k']
ac_range = range(ac_min, ac_max + 1, 1)
for i in ac_range:
    for j in weapons:
        for k in range(21):
            for x in range(j.atkCt):
                if ((k + j.atkBonus) >= i) or (k >= crit_roll):
                    j.totalDmg = j.totalDmg + ((j.dmgDie + 1) / 2) + j.dmgBonus
                if k >= crit_roll:
                    j.totalDmg = j.totalDmg + ((j.dmgDie + 1) / 2)
    for j in weapons:
        j.avgs.append(j.totalDmg / 20)
        j.totalDmg = 0

pyplot.figure(figsize=(24, 12))
pyplot.subplot(111)
legend = []
for k in range(len(weapons)):
    pyplot.plot(ac_range, weapons[k].avgs, colors[k])
    legend.append(weapons[k].name)
for k in range(len(weapons)):
    pyplot.plot(ac_range, weapons[k].avgs, colors[k] + 'o')
pyplot.legend(legend)
pyplot.title('Expected Dmg/Turn per Weapon by AC')
pyplot.xlabel('Armor Class')
pyplot.ylabel('Expected Damage per Turn')
pyplot.grid()
pyplot.show()
sys.exit(dialog.exec_())
