package memberAction;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;

import vofiles.EmpVOmember;
import Member.MemberInsertBean;

public class memberInsertAction extends JApplet implements ActionListener {

    private static final long serialVersionUID = 1L;
    private static String DELIMiTER = "\t";
    JTable table;
    DefaultTableModel model;
    private Graphics2D offs;

    String[] header = new String[] { "Name", "NickName", "Age", "Birth", "From", "Course",
            "Position", "Highschool", "Message" };

    String name = null;
    String nickname = null;
    String age = null;
    String birth = null;
    String from = null;
    String course = null;
    String position = null;
    String highschool = null;
    String message = null;

    EmpVOmember[][] data = new EmpVOmember[][] { {} };

    JPanel panel = new JPanel();
    JTextField numberTf = new JTextField(30);
    JTextField nicknameTf = new JTextField(30);
    JTextField birthDayTf = new JTextField(8);
    JTextField ageTf = new JTextField(8);
    JTextField fromTf = new JTextField(30);
    JTextField courseTf = new JTextField(100);
    JTextField positionTf = new JTextField(100);
    JTextField highschoolTf = new JTextField(20);
    JTextField messageTf = new JTextField(100);

    Button transitBtn = new Button("選択");
    Button searchBtn = new Button("新規登録ﾅｯｼｰ");

    //メッセージラベル（フィールドに設定）
    Label insertInfomations = new Label("下記の項目を入力してください。");
    Label labelNumber = new Label("名前：");
    Label labelBirthday = new Label("誕生日：");
    Label labelNickname = new Label("ニックネーム：");
    Label labelAge = new Label("年齢：");
    Label labelFrom = new Label("出身地：");
    Label labelCourse = new Label("所属コース：");
    Label labelPosition = new Label("ポジション：");
    Label labelHighschool = new Label("出身校：");
    Label labelMessage = new Label("メッセージ：");
    Choice cho;
    private String fileName = "Members.txt";

    public void init() {

        setLayout(null);

        // 背景色の設定
        Container c = getContentPane();
        c.setBackground(Color.WHITE);
        int w = getWidth();
        int h = getHeight();
        setLayout(new BorderLayout());

        // テーブルオブジェクトの生成
        model = new DefaultTableModel(data, header);
        table = new JTable(model);
        table.setGridColor(Color.BLUE);
        DefaultTableColumnModel columnModel = (DefaultTableColumnModel) table.getColumnModel();
        columnModel.getColumn(1).setPreferredWidth(20);
        // テーブルをレイアウトに追加
        JScrollPane sc = new JScrollPane(table);
        sc.setBackground(Color.WHITE);
        sc.setPreferredSize(new Dimension(w, h - 60));
        add(sc, BorderLayout.NORTH);
        //panel.setPreferredSize(new Dimension(w, 30));
        //panel.setLayout(new BorderLayout());
        //numberTf.setPreferredSize(new Dimension(10, 25));
        //panel.add(numberTf, BorderLayout.WEST);
        //birthDayTf.setPreferredSize(new Dimension(20, 25));
        //panel.add(birthDayTf, BorderLayout.CENTER);
        //panel.add(messageTf, BorderLayout.EAST);
        add(panel, BorderLayout.SOUTH);

        add(insertInfomations,BorderLayout.LINE_START);
        add(labelNumber, BorderLayout.LINE_START);
        add(numberTf, BorderLayout.LINE_START);
        add(labelNickname, BorderLayout.LINE_START);
        add(nicknameTf, BorderLayout.LINE_START);
        add(labelBirthday, BorderLayout.LINE_START);
        add(birthDayTf, BorderLayout.LINE_START);
        add(labelAge, BorderLayout.LINE_START);
        add(ageTf, BorderLayout.LINE_START);
        add(labelFrom, BorderLayout.LINE_START);
        add(fromTf, BorderLayout.LINE_START);
        add(labelCourse, BorderLayout.LINE_START);
        add(courseTf, BorderLayout.LINE_START);
        add(labelPosition, BorderLayout.LINE_START);
        add(positionTf, BorderLayout.LINE_START);
        add(labelHighschool, BorderLayout.LINE_START);
        add(highschoolTf, BorderLayout.LINE_START);
        add(labelMessage, BorderLayout.LINE_START);
        add(messageTf, BorderLayout.LINE_START);


        insertInfomations.setBounds(200, 150, 200, 20);
        labelNumber.setBounds(200, 175, 200, 20);
        numberTf.setBounds(400, 175, 200, 20);
        labelNickname.setBounds(200, 200, 200, 20);
        nicknameTf.setBounds(400, 200, 200, 20);
        labelBirthday.setBounds(200, 225, 200, 20);
        birthDayTf.setBounds(400, 225, 200, 20);
        labelAge.setBounds(200, 250, 200, 20);
        ageTf.setBounds(400,250, 200, 20);
        labelFrom.setBounds(200, 275, 200, 20);
        fromTf.setBounds(400, 275, 200, 20);
        labelCourse.setBounds(200, 300, 200, 20);
        courseTf.setBounds(400, 300, 200, 20);
        labelPosition.setBounds(200, 325, 200, 20);
        positionTf.setBounds(400, 325, 200, 20);
        labelHighschool.setBounds(200, 350, 200, 20);
        highschoolTf.setBounds(400, 350, 200, 20);
        labelMessage.setBounds(200, 375, 200, 20);
        messageTf.setBounds(400, 375, 200, 20);


//        labelBirthday.setBounds(200, 225, 100, 20);
//        labelMessage.setBounds(200, 275, 100, 20);
        //transitBtn.setPreferredSize(new Dimension(100, 30));
        transitBtn.setBounds(500, 500, 200, 20);
        transitBtn.addActionListener(this);
        searchBtn.setPreferredSize(new Dimension(100, 30));
        searchBtn.addActionListener(this);
        add(searchBtn, BorderLayout.LINE_START);
        searchBtn.setBounds(1, 2, 100, 20);
        cho = (Choice) add(new Choice());
        cho.add("--選択してください--");
        cho.add("1.全件表示");
        cho.add("2.１件検索");
        cho.add("3.新規登録");
        cho.add("4.削除更新");
        cho.add("5.書換更新");
        cho.add("6.メインメニューに戻る");
        add(transitBtn, BorderLayout.PAGE_END);
        add(cho,BorderLayout.SOUTH);
        cho.setBounds(200, 450, 200, 20);


        // テーブルの上下左右の余白設定
        add(Box.createHorizontalStrut(10), BorderLayout.EAST);
        add(Box.createHorizontalStrut(10), BorderLayout.WEST);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == searchBtn) {
            MemberInsertBean memberInsertBean = new MemberInsertBean();
            String checkNumber = numberTf.getText();
            String checkNickname = nicknameTf.getText();
            String checkBirthDay = birthDayTf.getText();
            String checkAge = ageTf.getText();
            String checkFrom = fromTf.getText();
            String checkCourse = courseTf.getText();
            String checkPosition = positionTf.getText();
            String checkHighschool = highschoolTf.getText();
            String checkMessAge = messageTf.getText();

            EmpVOmember data = new EmpVOmember();
            String[] emp = {checkNumber,checkNickname,checkBirthDay,checkAge,checkFrom,
                    checkCourse,checkPosition,checkHighschool,checkMessAge};
            data = memberInsertBean.executeInsertReturn(fileName, emp );

            //アプレット上ではインサートされているが、実際はテーブルに登録しない。
            if(data != null){
            model.removeRow(model.getRowCount() - 1);
            model.addRow(new String[] { data.getName(), data.getNickname(), data.getAge(), data.getBirth(),
                    data.getFrom(), data.getCourse(), data.getPosition(), data.getHighschool(), data.getMessage() });
            }else{
                model.removeRow(model.getRowCount() - 1);
                model.addRow(new String[] { null, null, null, null,
                        null, null, null, null, null });
            }

            System.out.println(cho + "," + cho.getSelectedItem() + cho.getSelectedIndex());
        }
        if (event.getSource() == transitBtn) {
            if (cho.getSelectedIndex() == 1) {
                System.out.println("全件表示");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 2) {
                System.out.println("１件表示");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 3) {
                System.out.println("新規登録");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 4) {
                System.out.println("削除");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 5) {
                System.out.println("更新");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 6) {
                System.out.println("戻る");
                System.out.println("Address was transfer");
            }
        }
    }

}
