import React from "react";
import {IWeapon, WeaponPiercing} from "../types/IUnit";
import {
    Grid2,
    Paper,
    styled,
    Table,
    TableBody,
    TableCell,
    tableCellClasses,
    TableContainer,
    TableHead,
    TableRow, ToggleButton, ToggleButtonGroup
} from "@mui/material";
import ArmorType from "./ArmorType";
import ArmorTypeNames from "../types/ArmorTypeValues";

interface IWeaponProps{
    weapon: IWeapon,
}

interface IWeaponState{
    currentTable: "dps" | "one hit",
}

class Weapon extends React.Component<IWeaponProps, any> {

    humanReadableName(unit: string) {
        const firstUpper = String(unit).charAt(0).toUpperCase() + String(unit).slice(1);
        return firstUpper.replaceAll('_', ' ').replace('.rgd', '');
    }

    constructor(props: any) {
        super(props);
        this.state = ({
            currentTable: 'dps'
        });
    }


    getPiercingK(armorType: string): number {
        const weaponPiercing = this.props.weapon.weaponPiercings.find((p) => p.armorType.name === armorType)
        return (typeof weaponPiercing !== "undefined" ? weaponPiercing.piercingValue : 10 ) / 100
    }

    handleChange = (e: React.MouseEvent,  selectedDamageTable: String | null) => {
        this.setState({
            currentTable: selectedDamageTable
        });
    }



    render() {

        const weapon = this.props.weapon


        const dpsK = (this.state.currentTable == "dps") ? weapon.accuracy * (1/(weapon.reloadTime - weapon.reloadTime % 0.125)) : 1

        const infLowPiercing = this.getPiercingK(ArmorTypeNames.InfantryLow)
        const infMedPiercing = this.getPiercingK(ArmorTypeNames.InfantryMedium)
        const infHighPiercing = this.getPiercingK(ArmorTypeNames.InfantryHigh)
        const infHeavyMedPiercing = this.getPiercingK(ArmorTypeNames.InfantryHeavyMedium)
        const infHeavyHighPiercing = this.getPiercingK(ArmorTypeNames.InfantryHeavyMedium)
        const demonPiercing = this.getPiercingK(ArmorTypeNames.DemonMedium)
        const demonHighPiercing = this.getPiercingK(ArmorTypeNames.DemonHigh)
        const commanderPiercing = this.getPiercingK(ArmorTypeNames.Commander)

        const airPiercing = this.getPiercingK(ArmorTypeNames.Air)
        const vehLowPiercing = this.getPiercingK(ArmorTypeNames.VehicleLow)
        const vehMedPiercing = this.getPiercingK(ArmorTypeNames.VehicleMedium)
        const vehHighPiercing = this.getPiercingK(ArmorTypeNames.VehicleHigh)
        const buildingLowPiercing = this.getPiercingK(ArmorTypeNames.BuildingLow)
        const buildingMedPiercing = this.getPiercingK(ArmorTypeNames.BuildingMedium)
        const buildingHighPiercing = this.getPiercingK(ArmorTypeNames.BuildingHigh)

        const getTotalDamage = (damagePiercing: number, isAir: boolean = false) => {

            var minDamage = damagePiercing * weapon.minDamage
            var maxDamage = damagePiercing * weapon.maxDamage

            if(minDamage < weapon.minDamageValue){
                minDamage = weapon.minDamageValue
            }
            if(maxDamage < weapon.minDamageValue){
                maxDamage = weapon.minDamageValue
            }

            const averageDmg = (minDamage + maxDamage) / 2

            return (averageDmg * dpsK).toFixed(2)
        }

        const getMoraleDamage = () => {
            return (weapon.moraleDamage * dpsK / 2).toFixed(2)
        }


        const StyledTableCell = styled(TableCell)(({ theme }) => ({
            [`&.${tableCellClasses.head}`]: {
                backgroundColor: "rgb(234, 234, 234)",
                color: theme.palette.common.white,
            },
            [`&.${tableCellClasses.body}`]: {
                fontSize: 14,
            },
        }));

        return (
            <div>
                <h4>{this.humanReadableName(weapon.name)}</h4>
                <Grid2  container spacing={2}>

                    <Grid2 size={3}>
                        <TableContainer component={Paper}>
                            <Table size="small" aria-label="a dense table">
                                <TableBody>
                                    <TableRow
                                        sx={{'&:last-child td, &:last-child th': {border: 0}}}
                                    >
                                        <TableCell component="th" scope="row">Base damage</TableCell>
                                        <TableCell component="th"
                                                   scope="row">{weapon.minDamage} {weapon.maxDamage !== weapon.minDamage && -weapon.maxDamage}
                                        </TableCell>
                                    </TableRow>
                                    <TableRow
                                        sx={{'&:last-child td, &:last-child th': {border: 0}}}
                                    >
                                        <TableCell component="th" scope="row">Accuracy</TableCell>
                                        <TableCell component="th" scope="row">{weapon.accuracy.toFixed(2)}
                                        </TableCell>
                                    </TableRow>
                                    <TableRow
                                        sx={{'&:last-child td, &:last-child th': {border: 0}}}
                                    >
                                        <TableCell component="th" scope="row">Accuracy moving</TableCell>
                                        <TableCell component="th"
                                                   scope="row">{weapon.setupTime === 0 ? (weapon.accuracy - weapon.accuracyReductionMoving).toFixed(2) : "-"}
                                        </TableCell>
                                    </TableRow>
                                    <TableRow
                                        sx={{'&:last-child td, &:last-child th': {border: 0}}}
                                    >
                                        <TableCell component="th" scope="row">Reload time</TableCell>
                                        <TableCell component="th"
                                                   scope="row">{weapon.reloadTime}
                                        </TableCell>
                                    </TableRow>
                                    <TableRow
                                        sx={{'&:last-child td, &:last-child th': {border: 0}}}
                                    >
                                        <TableCell component="th" scope="row">Max range</TableCell>
                                        <TableCell component="th"
                                                   scope="row">{weapon.maxRange  ? (weapon.maxRange) : "-"}
                                        </TableCell>
                                    </TableRow>
                                    <TableRow
                                        sx={{'&:last-child td, &:last-child th': {border: 0}}}
                                    >
                                        <TableCell component="th" scope="row">Setup time</TableCell>
                                        <TableCell component="th"
                                                   scope="row">{weapon.setupTime != 0 ? (weapon.setupTime).toFixed(2) : "-"}
                                        </TableCell>
                                    </TableRow>
                                </TableBody>
                            </Table>
                        </TableContainer>
                    </Grid2>
                    <Grid2 size={9}>
                        <ToggleButtonGroup
                            color="primary"
                            exclusive
                            value={this.state.currentTable}
                            onChange={this.handleChange}
                            aria-label="Platform"
                        >
                            <ToggleButton size="small" value="dps">Dps</ToggleButton>
                            <ToggleButton size="small" value="one hit">One hit average damage</ToggleButton>
                        </ToggleButtonGroup>
                        <TableContainer>
                        <Table sx={{minWidth: 600, marginTop: "10px"}}  size="small" aria-label="a dense table">
                            <TableHead>
                                <TableRow>
                                    <StyledTableCell ><ArmorType name={ArmorTypeNames.InfantryLow}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.InfantryMedium}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.InfantryHigh}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.InfantryHeavyMedium}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.InfantryHeavyHigh}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.DemonMedium}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.DemonHigh}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.Commander}/></StyledTableCell>
                                </TableRow>
                            </TableHead>
                            <TableBody>
                                <TableRow>
                                    <StyledTableCell>{getTotalDamage(infLowPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(infMedPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(infHighPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(infHeavyMedPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(infHeavyHighPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(demonPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(demonHighPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(commanderPiercing)}</StyledTableCell>
                                </TableRow>
                            </TableBody>
                            <TableHead>
                                <TableRow >
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.Air}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.VehicleLow}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.VehicleMedium}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.VehicleHigh}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.BuildingLow}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.BuildingMedium}/></StyledTableCell>
                                    <StyledTableCell><ArmorType name={ArmorTypeNames.BuildingHigh}/></StyledTableCell>
                                    <StyledTableCell><img style={{verticalAlign: "top"}} src="/images/ARM_Morale.webp"/></StyledTableCell>
                                </TableRow>
                            </TableHead>
                            <TableBody>
                                <TableRow>
                                    <StyledTableCell>{getTotalDamage(airPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(vehLowPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(vehMedPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(vehHighPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(buildingLowPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(buildingMedPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getTotalDamage(buildingHighPiercing)}</StyledTableCell>
                                    <StyledTableCell>{getMoraleDamage()}</StyledTableCell>
                                </TableRow>
                            </TableBody>
                        </Table>
                        </TableContainer>
                    </Grid2>
                </Grid2>
            </div>
        );
    }
}

export default Weapon;