package cn.jvtc.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner[] spFoot = new Spinner[3];
    private Spinner spFtype;
    private ArrayAdapter<String>[] adapters = new ArrayAdapter[3];
    private ArrayAdapter<String> adapter;
    private List<String>[] lFoot = new List[3];
    private EditText[] edtFoot = new EditText[3];
    private String[] stFoot = new String[3];
    private String[] stFtype = {"荤菜", "素菜", "配汤"};
    private String[][] stFName = {{"红烧排骨", "九江鱼块", "小炒肉", "爆炒猪肝", "干锅花菜"},
            {"酸辣土豆丝", "清炒时蔬", "油淋生菜", "鱼香茄子"},
            {"猪肝汤", "青菜汤", "紫菜蛋汤"}};
    private Button btnOk, btnAdd;
    private EditText edtFName;
    private int
            ids[][] = {{R.id.sp_foot1, R.id.sp_foot2, R.id.sp_foot3}, {R.id.edt_foot1, R.id.edt_foot2, R.id.edt_foot3}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spFtype = (Spinner) findViewById(R.id.sp_fType);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stFtype);
        spFtype.setAdapter(adapter);
        for (int i = 0; i < 3; i++) {
            spFoot[i] = (Spinner) findViewById(ids[0][i]);
            edtFoot[i] = (EditText) findViewById(ids[1][i]);
            lFoot[i] = new ArrayList<String>();
            for (int j = 0; j < stFName[i].length; j++)
                lFoot[i].add(stFName[i][j]);
            adapters[i] = new
                    ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lFoot[i]);
            spFoot[i].setAdapter(adapters[i]);
        }
        edtFName = (EditText) findViewById(R.id.edt_fName);
        btnOk = (Button) findViewById(R.id.btn_ok);
        btnAdd = (Button) findViewById(R.id.btn_add);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < 3; i++) {
                    stFoot[i] = spFoot[i].getSelectedItem().toString();
                    edtFoot[i].setText(stFtype[i] + "：" + stFoot[i]);
                }
            }
        });
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int positon = (int) spFtype.getSelectedItemId();
                String stAdd = edtFName.getText().toString();
                for (int i = 0; i < adapters[positon].getCount(); i++)
                    if (adapters[positon].getItem(i).toString().equals(stAdd)) {
                        Toast.makeText(MainActivity.this, "菜单中已存在相同菜名", Toast.LENGTH_SHORT).show();
                        return;
                    }
                adapters[positon].add(stAdd);
                Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}