package com.ecom.park_jy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CarNum_Input extends AppCompatActivity {   //車牌輸入
    Button btn_A, btn_B, btn_C, btn_D, btn_E, btn_F, btn_G, btn_H, btn_I, btn_J, btn_K, btn_L, btn_M, btn_N, btn_O, btn_P, btn_Q, btn_R, btn_S, btn_T, btn_U, btn_V, btn_W, btn_X, btn_Y, btn_Z;
    Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_back, btn_del, btn_dash, btn_gn, btn_lin;
    String carNumber = "";
    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    EditText edit;
    TextView t29;
    private boolean isMilitary = false;
    private boolean isTemporary = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_num__input);
        btn_A = (Button) findViewById(R.id.button27);
        btn_B = (Button) findViewById(R.id.button28);
        btn_C = (Button) findViewById(R.id.button29);
        btn_D = (Button) findViewById(R.id.button30);
        btn_E = (Button) findViewById(R.id.button31);
        btn_F = (Button) findViewById(R.id.button32);
        btn_G = (Button) findViewById(R.id.button33);
        btn_H = (Button) findViewById(R.id.button34);
        btn_I = (Button) findViewById(R.id.button35);
        btn_J = (Button) findViewById(R.id.button36);
        btn_K = (Button) findViewById(R.id.button37);
        btn_L = (Button) findViewById(R.id.button38);
        btn_M = (Button) findViewById(R.id.button39);
        btn_N = (Button) findViewById(R.id.button40);
        btn_O = (Button) findViewById(R.id.button41);
        btn_P = (Button) findViewById(R.id.button42);
        btn_Q = (Button) findViewById(R.id.button43);
        btn_R = (Button) findViewById(R.id.button44);
        btn_S = (Button) findViewById(R.id.button45);
        btn_T = (Button) findViewById(R.id.button46);
        btn_U = (Button) findViewById(R.id.button47);
        btn_V = (Button) findViewById(R.id.button48);
        btn_W = (Button) findViewById(R.id.button49);
        btn_X = (Button) findViewById(R.id.button50);
        btn_Y = (Button) findViewById(R.id.button51);
        btn_Z = (Button) findViewById(R.id.button52);

        btn_0 = (Button) findViewById(R.id.button62);
        btn_1 = (Button) findViewById(R.id.button59);
        btn_2 = (Button) findViewById(R.id.button60);
        btn_3 = (Button) findViewById(R.id.button61);
        btn_4 = (Button) findViewById(R.id.button56);
        btn_5 = (Button) findViewById(R.id.button57);
        btn_6 = (Button) findViewById(R.id.button58);
        btn_7 = (Button) findViewById(R.id.button53);
        btn_8 = (Button) findViewById(R.id.button54);
        btn_9 = (Button) findViewById(R.id.button55);

        btn_back = (Button) findViewById(R.id.button67);
        btn_del = (Button) findViewById(R.id.button66);
        btn_dash = (Button) findViewById(R.id.button63);
        btn_gn = (Button) findViewById(R.id.button64);
        btn_lin = (Button) findViewById(R.id.button65);
        edit = (EditText) findViewById(R.id.editText);
        edit.setInputType(InputType.TYPE_NULL);

        t29 = (TextView) findViewById(R.id.textView29);


        setListeners();

    }


    private void setListeners() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (carNumber == null || carNumber.length() == 0) {
                    Toast.makeText(CarNum_Input.this, "請輸入車牌", Toast.LENGTH_LONG).show();
                } else if (!carNumber.contains("-")) {
                    Toast.makeText(CarNum_Input.this, "車牌請用\"-\"符號分隔", Toast.LENGTH_LONG).show();
                } else if (carNumber.split("-").length <= 1) {
                    Toast.makeText(CarNum_Input.this, "車牌輸入有誤", Toast.LENGTH_LONG).show();
                } else {
                    bundle.putString("ITEM", carNumber);
                    bundle.putBoolean("ISMILI", isMilitary);
                    bundle.putBoolean("ISTEMP", isTemporary);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                if (carNumber.length() > 0) {
                    carNumber = carNumber.substring(0, carNumber.length() - 1);
                    edit.setText(carNumber);
                }
            }
        });

        btn_gn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                if (isMilitary) {
                    isMilitary = false;
                    t29.setText("");
                } else {
                    isTemporary = false;
                    isMilitary = true;
                    t29.setText("軍");
                }
            }
        });

        btn_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                if (isTemporary) {
                    isTemporary = false;
                    t29.setText("");
                } else {
                    isMilitary = false;
                    isTemporary = true;
                    t29.setText("臨");
                }
            }
        });

        btn_dash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                if (!carNumber.contains("-")) {
                    carNumber += "-";
                }
                edit.setText(carNumber);
            }
        });

        btn_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "0";
                edit.setText(carNumber);
            }
        });

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "1";
                edit.setText(carNumber);
            }
        });

        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "2";
                edit.setText(carNumber);
            }
        });

        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "3";
                edit.setText(carNumber);
            }
        });

        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "4";
                edit.setText(carNumber);
            }
        });

        btn_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "5";
                edit.setText(carNumber);
            }
        });

        btn_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "6";
                edit.setText(carNumber);
            }
        });

        btn_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "7";
                edit.setText(carNumber);
            }
        });

        btn_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "8";
                edit.setText(carNumber);
            }
        });

        btn_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "9";
                edit.setText(carNumber);
            }
        });

        btn_A.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "A";
                edit.setText(carNumber);
            }
        });

        btn_B.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "B";
                edit.setText(carNumber);
            }
        });

        btn_C.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "C";
                edit.setText(carNumber);
            }
        });

        btn_D.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "D";
                edit.setText(carNumber);
            }
        });

        btn_E.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "E";
                edit.setText(carNumber);
            }
        });

        btn_F.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "F";
                edit.setText(carNumber);
            }
        });

        btn_G.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "G";
                edit.setText(carNumber);
            }
        });

        btn_H.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "H";
                edit.setText(carNumber);
            }
        });

        btn_I.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "I";
                edit.setText(carNumber);
            }
        });

        btn_J.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "J";
                edit.setText(carNumber);
            }
        });

        btn_K.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "K";
                edit.setText(carNumber);
            }
        });

        btn_L.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "L";
                edit.setText(carNumber);
            }
        });

        btn_M.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "M";
                edit.setText(carNumber);
            }
        });

        btn_N.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "N";
                edit.setText(carNumber);
            }
        });

        btn_O.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "O";
                edit.setText(carNumber);
            }
        });

        btn_P.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "P";
                edit.setText(carNumber);
            }
        });

        btn_Q.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "Q";
                edit.setText(carNumber);
            }
        });

        btn_R.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "R";
                edit.setText(carNumber);
            }
        });

        btn_S.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "S";
                edit.setText(carNumber);
            }
        });

        btn_T.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "T";
                edit.setText(carNumber);
            }
        });

        btn_U.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "U";
                edit.setText(carNumber);
            }
        });

        btn_V.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "V";
                edit.setText(carNumber);
            }
        });

        btn_W.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "W";
                edit.setText(carNumber);
            }
        });

        btn_X.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "X";
                edit.setText(carNumber);
            }
        });

        btn_Y.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "Y";
                edit.setText(carNumber);
            }
        });

        btn_Z.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carNumber = edit.getText().toString();
                carNumber += "Z";
                edit.setText(carNumber);
            }
        });
    }
}
