/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package d.d;

/**
 *
 * @author pospis
 */

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class HManagment {
    HMpanel panel;
    MainFrame frame;
    

    static final int Main = 1;
    static final int HeroesEditor = 2;
    static final int HeroesManagment = 3;
    static final int Battle = 4;
    static final int Creatures = 5;
    static final int Map = 6;
    
    static final int name=0;
    static final int race=1;
    static final int profession=2;
    static final int level=3;
    static final int height=5;
    static final int health=4;
    static final int mass=6;
    static final int power=7;
    static final int ability=8;
    static final int hardness=9;
    static final int intelligence=10;
    static final int charisma=11;
    static final int others=12;
       
    Hero myHero;
    ArrayList<Item> items;
    int ScreenWidth;
    int ScreenHeight;
   
    
    public HManagment(MainFrame frame) throws FileNotFoundException, IOException{
        this.frame=frame;     
        panel = new HMpanel();
    }
    
    public JPanel getPanel(){
        return panel;
    }
    
    class HMpanel extends JPanel{
        
        Manager m;
        
        public HMpanel() throws FileNotFoundException, IOException{
            ScreenWidth=frame.getWidth();
            ScreenHeight=frame.getHeight();
            
            JMenuBar menuBar = new JMenuBar();
            frame.setJMenuBar(menuBar);
            JMenu file = new JMenu("File");
            menuBar.add(file);

            JMenuItem open = new JMenuItem("Open");
            JMenuItem save = new JMenuItem("Save");
            JMenuItem exit = new JMenuItem("Return to Menu");

            file.add(open);
            file.add(save);
            file.addSeparator();
            file.add(exit);
            frame.setVisible(true);
            
            BufferedReader br = new BufferedReader(new FileReader("resources/armor.txt"));
            String str=br.readLine();
            items=new ArrayList<Item>();
            while(str!=null){
                items.add(new Item(str,"armor"));
                str=br.readLine();
            }
            br.close();
            br=new BufferedReader(new FileReader("resources/weapons.txt"));
            str=br.readLine();
            while(str!=null){
                items.add(new Item(str,"weapon"));
                str=br.readLine();
            }
            br.close();
            br=new BufferedReader(new FileReader("resources/items.txt"));
            str=br.readLine();
            while(str!=null){
                items.add(new Item(str,"item"));
                str=br.readLine();
            }
            br.close();
            
            
            
            open.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        open();
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(HManagment.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(HManagment.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            
            save.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        save();
                    } catch (IOException ex) {
                        Logger.getLogger(HManagment.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            exit.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    frame.ChangePanel(Main);
                }
            });
            
            JOptionPane.showMessageDialog(frame, "Load a hero!");
            
        }
        
        class Manager extends JPanel implements ActionListener{
            
            JButton levelup;
            JLabel llevel;
            JLabel loff;
            JLabel ldef;
            JButton Ainv;
            JButton Dinv;
            JLabel lHealth;
            JTextArea all;
            JButton bcoins;
            JLabel lcoins;
            JButton rightb;
            JButton armorb;
            JButton leftb;
            JLabel larmor;
            JLabel llhand;
            JLabel lrhand;
            
            int actualarmor;
            int actualoff;
            
            boolean righthand=false;
            boolean lefthand=false;
            private final JButton bothers;
            
            public Manager(){
            
            setLayout(new GridLayout(1,3));
            
            final JPanel center = new JPanel();
            JPanel left = new JPanel();
            JPanel right = new JPanel();
            
            center.setPreferredSize(new Dimension(ScreenHeight,ScreenWidth/3));
            left.setPreferredSize(new Dimension(ScreenHeight,ScreenWidth/3));
            right.setPreferredSize(new Dimension(ScreenHeight,ScreenWidth/3));
            
            left.setOpaque(false);
            right.setOpaque(false);
            center.setOpaque(false);
            
            add(left);
            add(center);
            add(right);
           
            
            /*******************************LEFT*******************************/
            
            left.setLayout(new GridLayout(6,2));
            left.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
            
            JLabel lname = new JLabel("Name :  "+myHero.atr.get(name).toString());
            JLabel lrace = new JLabel("Race :  "+myHero.atr.get(race).toString());
            lHealth = new JLabel("Health :  "+myHero.atr.get(health).toString());
            JLabel lprofession = new JLabel("Profession :  "+myHero.atr.get(profession).toString());
            JLabel heighthero = new JLabel("Height :  "+myHero.atr.get(height).toString());
            JLabel lmass =new JLabel("Mass :  "+myHero.atr.get(mass).toString());
            JLabel lpower = new JLabel("Power :  "+myHero.atr.get(power).toString() );
            JLabel lability = new JLabel("Ability :  "+myHero.atr.get(ability).toString());
            JLabel lhardness = new JLabel("Hardness :  "+myHero.atr.get(hardness).toString());
            JLabel lintelligence = new JLabel("Intelligence :  "+myHero.atr.get(intelligence).toString());
            JLabel lcharisma = new JLabel("Charisma :  "+myHero.atr.get(charisma).toString());
            bothers = new JButton("Others");
            
            lHealth.setFont(heighthero.getFont().deriveFont(16.0f));
            lname.setFont(heighthero.getFont().deriveFont(16.0f));
            lrace.setFont(heighthero.getFont().deriveFont(16.0f));
            lprofession.setFont(heighthero.getFont().deriveFont(16.0f));
            heighthero.setFont(heighthero.getFont().deriveFont(16.0f));
            lmass.setFont(heighthero.getFont().deriveFont(16.0f));
            lpower.setFont(heighthero.getFont().deriveFont(16.0f));
            lability.setFont(heighthero.getFont().deriveFont(16.0f));
            lhardness.setFont(heighthero.getFont().deriveFont(16.0f));
            lintelligence.setFont(heighthero.getFont().deriveFont(16.0f));
            lcharisma.setFont(heighthero.getFont().deriveFont(16.0f));
            
            
            left.add(lname);
            left.add(lpower);
            left.add(lrace);
            left.add(lHealth);
            left.add(lability);
            left.add(lprofession);
            left.add(lhardness);
            left.add(heighthero);
            left.add(lintelligence);
            left.add(lmass);
            left.add(lcharisma);
            left.add(bothers);
            
            bothers.addActionListener(this);
            
            /********************************CENTER****************************/
            
            center.setLayout(new BorderLayout());
            center.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            
            llevel = new JLabel("Level :  "+myHero.atr.get(level).toString());
            levelup = new JButton("LEVEL UP");
            levelup.setMaximumSize(new Dimension(ScreenWidth/10, ScreenHeight/25));
            JPanel NPanel = new JPanel();
            NPanel.setLayout(new GridLayout(1,2));
            llevel.setFont(llevel.getFont().deriveFont(16.0f));
            center.add(NPanel,BorderLayout.NORTH);
            NPanel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
            NPanel.setPreferredSize(new Dimension(200,200));
            NPanel.add(llevel);
            NPanel.add(levelup);
            NPanel.setVisible(true);
            NPanel.setOpaque(false);
            
            levelup.addActionListener(this);
            
            
            JPanel CPanel = new JPanel();
            CPanel.setLayout(new GridLayout(5,2));
            CPanel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
            
            larmor = new JLabel("Armor: Nothing");
            armorb = new JButton("Change"); 
            armorb.setMaximumSize(new Dimension(ScreenWidth/5, ScreenHeight/10));
            armorb.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
            larmor.setBorder(BorderFactory.createEmptyBorder(0, 50, 0, 0));
            llhand = new JLabel("Left Hand: Nothing");
            leftb = new JButton("Change");    
            leftb.setMaximumSize(new Dimension(ScreenWidth/5, ScreenHeight/10));
            leftb.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
            llhand.setBorder(BorderFactory.createEmptyBorder(0,50, 0, 0));
            lrhand = new JLabel("Right Hand: Nothing");
            rightb= new JButton("Change");    
            rightb.setMaximumSize(new Dimension(ScreenWidth/5, ScreenHeight/10));
            rightb.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
            lrhand.setBorder(BorderFactory.createEmptyBorder(0, 50, 0, 0));
            loff = new JLabel("Ofensive : "+myHero.off);
            ldef = new JLabel("Defend : "+myHero.def);
            loff.setBorder(BorderFactory.createEmptyBorder(50, 50, 0, 0));
            ldef.setBorder(BorderFactory.createEmptyBorder(50, 50, 0, 0));
            loff.setFont(heighthero.getFont().deriveFont(16.0f));
            ldef.setFont(heighthero.getFont().deriveFont(16.0f));

            CPanel.add(larmor);
            CPanel.add(armorb);
            CPanel.add(llhand);
            CPanel.add(leftb);
            CPanel.add(lrhand);
            CPanel.add(rightb);
            CPanel.add(loff);
            CPanel.add(ldef);
            
            armorb.addActionListener(this);
            leftb.addActionListener(this);
            rightb.addActionListener(this);
            
            CPanel.setVisible(true);
            center.add(CPanel,BorderLayout.CENTER);
            
            CPanel.setOpaque(false);
            /********************************Right******************************/
            
            right.setLayout(new BoxLayout(right,BoxLayout.Y_AXIS));
            right.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            
            lcoins = new JLabel("Coins: "+myHero.coins);
            lcoins.setFont(lcoins.getFont().deriveFont(16.0f));
            bcoins = new JButton("Add coins");
            Ainv = new JButton("Add to inventory");
            Dinv = new JButton("Delete from inventory");
            lcoins.setBorder(BorderFactory.createEmptyBorder(20, 100, 50, 150));
            lcoins.setPreferredSize(new Dimension (150,150));
            Ainv.setMaximumSize(new Dimension(300, 50));
            Dinv.setMaximumSize(new Dimension(300, 50));
            all = new JTextArea();
            all.setEditable(false);
            Dinv.addActionListener(this);
            Ainv.addActionListener(this);
            bcoins.addActionListener(this);
            
            JLabel inventory = new JLabel("Inventory: ");
            
            
            
            all.setOpaque(false);
            
            
            right.add(lcoins,BorderLayout.NORTH);
            right.add(bcoins);
            right.add(Ainv);
            right.add(Dinv);
            
            right.add(Box.createRigidArea(new Dimension(0,20)));
            right.add(inventory);
            right.add(Box.createRigidArea(new Dimension(0,10)));
            right.add(all);
            
            for (int i = 0; i < myHero.it.size(); i++) {
                all.append( myHero.it.get(i).name+"\n");
            }

            center.setVisible(true);
            left.setVisible(true);
            right.setVisible(true);
                    
            repaint();
            
            
            /******************************************************************/
            
            actualoff=myHero.off;
            actualarmor=myHero.def;

            }
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==levelup){
                    final JFrame f = new JFrame("LEVEL UP!");
                    f.setSize(200,300);
                    
                    JPanel p = new JPanel();
                    f.add(p);
                    
                    p.add(new JLabel("Write a dice number:"));
                    final JTextField tf = new JTextField();
                    tf.setPreferredSize(new Dimension(40,30));
                    p.add(tf);
                    JButton b = new JButton("ok");
                    p.add(b);
                    b.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            myHero.atr.set(level, String.valueOf(Integer.parseInt(myHero.atr.get(level))+1));
                            llevel.setText("Level :  "+myHero.atr.get(level).toString());
                            changeHealth(Integer.parseInt(tf.getText()));
                            lHealth.setText("Health :  "+myHero.atr.get(health).toString());
                            f.dispose();
                        }
                    });
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                    
                }
                if(e.getSource()==Dinv){
                    myHero.deleteinv((String)all.getSelectedText());
                    all.setText("");
                    for (int i = 0; i < myHero.it.size(); i++) {
                        all.append( myHero.it.get(i).name+"\n");
                    }
                }
                if(e.getSource()==Ainv){
                    final JFrame f = new JFrame("Add to inventory");
                    f.setSize(200,300);
                    
                    JPanel p = new JPanel();
                    f.add(p);
                    
                    p.add(new JLabel("Choose item"));
                    final JComboBox combo = new JComboBox();
                    combo.setMaximumSize(new Dimension(400,100));
                    p.add(combo);
                    for (int i = 0; i < items.size(); i++) {
                        combo.addItem( items.get(i).name);
                    }
                    final JButton b = new JButton("Ok");
                    p.add(b);
                    b.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            if(e.getSource()==b){
                                myHero.it.add(finditemname((String) combo.getSelectedItem()));
                                all.append(myHero.it.get(myHero.it.size()-1).name+"\n");
                            }
                            f.dispose();
                        }
                    });
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                }
                
                if(e.getSource()==bcoins){
                   final JFrame f = new JFrame("Coins");
                    f.setSize(200,300);
                    
                    JPanel p = new JPanel();
                    f.add(p);
                    
                    p.add(new JLabel("Choose amount"));
                    final JTextField tf = new JTextField();
                    tf.setPreferredSize(new Dimension(40,30));
                    p.add(tf);
                    JButton b = new JButton("Ok");
                    p.add(b);
                    b.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {

                            myHero.coins=Integer.parseInt(tf.getText());
                            lcoins.setText("Coins :  "+myHero.coins);
                            f.dispose();
                        }
                    });
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                }
                
                if(e.getSource()==armorb){
                    final JFrame f = new JFrame("Armor");
                    f.setSize(200,300);
                    
                    JPanel p = new JPanel();
                    f.add(p);
                    
                    p.add(new JLabel("Choose item"));
                    final JComboBox combo = new JComboBox();
                    combo.setMaximumSize(new Dimension(400,100));
                    p.add(combo);
                    combo.addItem("Nothing");
                    for (int i = 0; i < myHero.it.size(); i++) {
                        if("armor".equals(myHero.it.get(i).type) && myHero.it.get(i).c<10){
                            combo.addItem(myHero.it.get(i).name); 
                        }
                    }
                    final JButton b = new JButton("ok");
                    p.add(b);
                    b.addActionListener(new ActionListener() {
                        
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            if(e.getSource()==b){
                                String str=(String)combo.getSelectedItem();
                                larmor.setText("Armor: "+str);
                                if(str.equals("Nothing")){
                                    myHero.armor=null;
                                }else{
                                    myHero.armor=new Item(fin(str));
                                }
                                ldef.setText("Defend: "+myHero.recalculatearmor());
                                
                            }
                            f.dispose();
                        }
                    });
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                }
                
                if(e.getSource()==leftb){
                    final JFrame f = new JFrame("Left Hand");
                    f.setSize(200,300);
                    
                    JPanel p = new JPanel();
                    f.add(p);
                    
                    p.add(new JLabel("Choose item"));
                    final JComboBox combo = new JComboBox();
                    combo.setMaximumSize(new Dimension(400,100));
                    p.add(combo);
                    combo.addItem("Nothing");
                    for (int i = 0; i < myHero.it.size(); i++) {
                        if(myHero.rightHand!=myHero.it.get(i)){
                            if("weapon".equals(myHero.it.get(i).type)){
                               if(myHero.it.get(i).c>100 && !righthand){ 
                                combo.addItem(myHero.it.get(i).name); 
                               }
                               if(myHero.it.get(i).c<100){
                                  if(myHero.rightHand == null){ 
                                   combo.addItem(myHero.it.get(i).name);
                                  }else{
                                      if(myHero.rightHand.c<100){
                                          combo.addItem(myHero.it.get(i).name);
                                      }
                                  }
                               }
                            }
                            if("armor".equals(myHero.it.get(i).type) && myHero.it.get(i).c>10){
                                if(myHero.rightHand == null){
                                  combo.addItem(myHero.it.get(i).name); 
                              }else{
                               if(myHero.rightHand.c<100){
                                   combo.addItem(myHero.it.get(i).name);
                               }
                              }
                            }
                        }
                    }
                    final JButton b = new JButton("Ok");
                    p.add(b);
                    
                    b.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            if(e.getSource()==b){
                                String str=(String)combo.getSelectedItem();
                                llhand.setText("Left Hand: "+str);
                                if(str.equals("Nothing")){
                                    myHero.leftHand=null;
                                    lefthand=false;
                                }else{
                                    myHero.leftHand=(fin(str));
                                    lefthand=true;
                                }
                                ldef.setText("Defend: "+myHero.recalculatearmor());
                                loff.setText("Ofenssive: "+myHero.recalculateoff());
                                
                            }
                            f.dispose();
                        }
                    });
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                }
                if(e.getSource()==rightb){
                    final JFrame f = new JFrame("Right Hand");
                    f.setSize(200,300);
                    
                    JPanel p = new JPanel();
                    f.add(p);
                    
                    p.add(new JLabel("Choose item"));
                    final JComboBox combo = new JComboBox();
                    combo.setMaximumSize(new Dimension(400,100));
                    p.add(combo);
                    combo.addItem("Nothing");
                    for (int i = 0; i < myHero.it.size(); i++) {
                       if(myHero.leftHand!=myHero.it.get(i)){
                        if("weapon".equals(myHero.it.get(i).type)){
                           if(myHero.it.get(i).c>100 && !lefthand){ 
                            combo.addItem(myHero.it.get(i).name); 
                           }
                           if(myHero.it.get(i).c<100){
                              if(myHero.leftHand == null){
                                  combo.addItem(myHero.it.get(i).name); 
                              }else{
                               if(myHero.leftHand.c<100){
                                   combo.addItem(myHero.it.get(i).name); 
                               }
                              }
                           }
                        }
                        if("armor".equals(myHero.it.get(i).type) && myHero.it.get(i).c>10){
                            if(myHero.leftHand == null){
                                  combo.addItem(myHero.it.get(i).name); 
                              }else{
                               if(myHero.leftHand.c<100){
                                   combo.addItem(myHero.it.get(i).name);
                               }
                              }
                            
                        }
                       }
                    }
                    final JButton b = new JButton("ok");
                    p.add(b);
                    
                    b.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            if(e.getSource()==b){
                                String str=(String)combo.getSelectedItem();
                                lrhand.setText("Right Hand: "+str);
                                if(str.equals("Nothing")){
                                    myHero.rightHand=null;
                                    righthand=false;
                                }else{
                                    myHero.rightHand=(fin(str));
                                    righthand=true;
                                }
                                ldef.setText("Defend: "+myHero.recalculatearmor());
                                loff.setText("Ofenssive: "+myHero.recalculateoff());
                                
                            }
                            f.dispose();
                        }
                    });
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                }
                if(e.getSource()==bothers){
                    final JFrame f = new JFrame("Others");
                    f.setSize(200,300);
                    
                    JPanel p = new JPanel();
                    f.add(p);
                    
                    p.add(new JLabel("Others abilities: "));
                    final JTextArea t = new JTextArea();
                    t.setText(myHero.atr.get(others));
                    t.setSize(100,200);
                    JButton addothers = new JButton("Add ability");
                    p.add(t);
                    p.add(addothers);
                    addothers.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            final JFrame fr = new JFrame("Add others");
                            fr.setSize(200,300);
                    
                    
                        JPanel p = new JPanel();
                        p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
                        fr.add(p);

                        p.add(new JLabel("Write ability: "));
                        final JTextField tf = new JTextField();
                        tf.setMaximumSize(new Dimension(300,40));
                        p.add(tf);
                        JButton b = new JButton("Ok");
                        p.add(b);
                        b.addActionListener(new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent arg0) {
                                myHero.atr.set(others, myHero.atr.get(others)+" "+tf.getText());
                                fr.dispose();
                                f.dispose();
                            }
                        });
                            fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                            fr.setVisible(true);
                        }
                    });
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                }
                
            }
            private void changeHealth(int x) {
                String pro = myHero.atr.get(profession).toString();    
                if("Warrior".equals(pro)){
                    myHero.atr.set(health, String.valueOf(Integer.parseInt(myHero.atr.get(health))+x));
                }
                if("Borderer".equals(pro)){
                    myHero.atr.set(health, String.valueOf(Integer.parseInt(myHero.atr.get(health))+x+2));
                }
                if("Alchemist".equals(pro)){
                    myHero.atr.set(health, String.valueOf(Integer.parseInt(myHero.atr.get(health))+x+1));
                }else{
                    myHero.atr.set(health, String.valueOf(Integer.parseInt(myHero.atr.get(health))+x));
                }
                
            }
            
            @Override
             protected void paintComponent(Graphics g) {
            super.paintComponent(g);

                g.drawImage(new ImageIcon("resources/edit3.jpg").getImage(),0,0, this.getWidth(), this.getHeight(), null);

            }
            
        }
        
        private void open() throws FileNotFoundException, IOException{
            
            JPanel op = new JPanel();
            op.setLayout(new BorderLayout());
            JTextArea log = new JTextArea(5, 20);
            log.setMargin(new Insets(5, 5, 5, 5));
            log.setEditable(false);
            JScrollPane logScrollPane = new JScrollPane(log);
            JFileChooser fc = new JFileChooser();
            op.add(logScrollPane, BorderLayout.CENTER);
            
            int returnVal = fc.showOpenDialog(op);
            
            if (returnVal == JFileChooser.APPROVE_OPTION){
                
                File file = fc.getSelectedFile();
                BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
                String str=null;
                String[] strline;
                myHero = new Hero();
                for (int i = 0; i < 13; i++) {
                    str=br.readLine();
                    strline=str.split(":");
                    myHero.atr.add(strline[1]);
                }
                str=br.readLine();
                myHero.coins=Integer.parseInt(str);
                str=br.readLine();
                while(str!=null){
                    myHero.it.add(finditemc(Integer.parseInt(str)));
                    str=br.readLine();
                }
                int off=Integer.parseInt(myHero.atr.get(power))/2 -5;
                    myHero.off=off;
                int def=Integer.parseInt(myHero.atr.get(ability))/2 -5;
                    myHero.def=def;
                  
                m = new Manager();
                frame.add(m);
                frame.setVisible(true);
            
            }
        }
        
        private void save() throws IOException{
            JOptionPane.showMessageDialog(frame, "Saved!");
                BufferedWriter output = new BufferedWriter(new FileWriter("resources/heroes/"+myHero.atr.get(name)+".hero"));
                output.write("Name:"+myHero.atr.get(name));
                output.newLine();
                output.write("Race:"+myHero.atr.get(race));
                output.newLine();
                output.write("Profession:"+myHero.atr.get(profession));
                output.newLine();
                output.write("Level:"+myHero.atr.get(level));
                output.newLine();
                output.write("Health:"+myHero.atr.get(health));
                output.newLine();
                output.write("Height:"+myHero.atr.get(height));
                output.newLine();
                output.write("Mass:"+myHero.atr.get(mass));
                output.newLine();
                output.write("Power:"+myHero.atr.get(power));
                output.newLine();
                output.write("Ability:"+myHero.atr.get(ability));
                output.newLine();
                output.write("Hardness:"+myHero.atr.get(hardness));
                output.newLine();
                output.write("Intelligence:"+myHero.atr.get(intelligence));
                output.newLine();
                output.write("Charisma:"+myHero.atr.get(charisma));
                output.newLine();
                output.write("Others:"+myHero.atr.get(others));
                output.newLine();
                output.write(""+myHero.coins);
                
                for (int i = 0; i < myHero.it.size(); i++) {
                    output.newLine();
                    output.write(""+myHero.it.get(i).c);
                
                }
                output.close();
                repaint();
            
        }
        
        private Item finditemc(int code){
            for (int i = 0; i < items.size(); i++) {
                if(code==(items.get(i).c)){
                    return items.get(i);
                }
            }
            return null;
        }
        
        private Item finditemname(String str){
            for (int i = 0; i < items.size(); i++) {
                if(str.equals(items.get(i).name)){
                    Item it = new Item(items.get(i));
                    return it;
                }
            }
            return null;
        }
        private Item fin(String str){
            for (int i = 0; i < items.size(); i++) {
                if(str.equals(items.get(i).name)){
                    return items.get(i);
                }
            }
            return null;
        }
        
        @Override
             protected void paintComponent(Graphics g) {
            super.paintComponent(g);

                g.drawImage(new ImageIcon("resources/edit3.jpg").getImage(),0,0, this.getWidth(), this.getHeight(), null);

            }
    }
    
    class Hero{
        
        int coins;
        int off;
        int def;
        
        Item armor=null;
        Item leftHand=null;
        Item rightHand=null;
        
        ArrayList<String> atr = new ArrayList<String>();
        ArrayList<Item> it = new ArrayList<Item>();
        
        public int recalculatearmor(){
            int x =def;
            if(armor != null){
                x+=armor.def;
            }
            if(leftHand != null){
                x+=leftHand.def;
            }
            if(rightHand != null){
                x+=rightHand.def;
            }
            if(x>0){
                return x;
            }else{
                return 0;
            }
        }
        public int recalculateoff(){
            int x =off;
            if(leftHand != null){
                x+=leftHand.power;
            }
            if(rightHand != null){
                x+=rightHand.power;
            }
            if(x>0){
                return x;
            }else{
                return 0;
            }
        }
        
        public void deleteinv(String item){
            for (int i = 0; i < it.size(); i++) {
                if(item.equals(it.get(i).name)){
                    it.remove(i);
                    return;
                }
            }
        }
    }
    class Item{
        String name;
        String type;
        int c=0;
        
        int cost=0;
        int power=0;
        int off=0;
        int def=0;
     
        int range[]; 
        
        public Item (Item it){
            this.name=it.name;
            this.type=it.type;
            this.c=it.c;
            this.cost=it.cost;
            this.power=it.power;
            this.off=it.off;
            this.def=it.def;
            this.range=it.range;
            
        }
        

        public Item(String item, String type){
            String[] str = item.split(" ");
            if("armor".equals(type)){
                this.type=type;
                c=Integer.parseInt(str[0]);
                name=str[1];
                cost=Integer.parseInt(str[2]);
                if(c>10){
                    def=c-10;
                }else{
                    def=c;
                }
            }
            if("weapon".equals(type)){
                this.type=type;
                c=Integer.parseInt(str[0]);
                name=str[1];
                power= Integer.parseInt(str[2]);
                off=Integer.parseInt(str[3]);
                if(c< 1000){
                    def=Integer.parseInt(str[4]);
                    cost=Integer.parseInt(str[5]);
                }else{
                    range= new int[3];
                    range[0]=Integer.parseInt(str[4]);
                    range[1]=Integer.parseInt(str[5]);
                    range[2]=Integer.parseInt(str[6]);
                    cost=Integer.parseInt(str[7]);
                    
                }
            }
            if("item".equals(type)){
                this.type=type;
                c=Integer.parseInt(str[0]);
                name=str[1];
                cost=Integer.parseInt(str[2]);
            }
        }
    
    }
    
    
}
