package com.example.Fruits;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

/**
 *  建立sqlite数据库，进行用户名和密码的存储
 */
public class CollectActivity extends AppCompatActivity implements View.OnClickListener{

    //编辑框，输入
    EditText username;
    EditText password;
    //EditText selectuser;

    //结果文本框
    TextView result;

    //3个按钮
    Button buttonadd;
    Button buttondelete;
    Button buttonselect;

    //帮助建立sqlite数据库
    SQLiteOpenHelper mysqlhelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collect_activity);

        init();
        buttonadd.setOnClickListener(this);
        buttondelete.setOnClickListener(this);
        buttonselect.setOnClickListener(this);
    }

    public void init(){
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        //selectuser = findViewById(R.id.selectuser);

        result = findViewById(R.id.result);

        buttonadd = findViewById(R.id.buttonadd);
        buttondelete = findViewById(R.id.buttondelete);
        buttonselect = findViewById(R.id.buttonselect);
//        mysqlhelper = new MyDBHelper(MainActivity.this, "MyDatabase.db", null, 666);
//        //SQLiteDatabase db = mysqlhelper.getWritableDatabase();
    }

    @Override
    public void onClick(View v) {

        mysqlhelper = new MyDBHelper(CollectActivity.this, "MyDatabase.db", null, 666);
        SQLiteDatabase db;
        String username1 = null;
        String password1 = null;
        Log.v("start","按钮事件：");
        switch(v.getId()){
            case R.id.buttonadd:
                db = mysqlhelper.getWritableDatabase();
                username1 = username.getText().toString();
                password1 = password.getText().toString();
                if(username1==null&&username1==""&&username1==" "){
                    Toast.makeText(CollectActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
                    Log.v("start","添加失败");
                }else if(password1==null){
                    Toast.makeText(CollectActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();

                }else {
                    db.execSQL("insert into user(username,password) values(?,?)", new Object[]{username1, password1});
                    Toast.makeText(CollectActivity.this,"添加成功",Toast.LENGTH_LONG).show();
                    //result.setText("查询结果为：");
                    //result.append("标题"+username1+"内容"+password1);
                }
                db.close();
                break;

            case R.id.buttondelete:
                db = mysqlhelper.getWritableDatabase();
                String usernamed = username.getText().toString();

                db.execSQL("delete from user where username=?",new Object[]{usernamed});
                db.close();
                Toast.makeText(CollectActivity.this,"删除成功",Toast.LENGTH_LONG).show();
                break;
            case R.id.buttonselect:
                result.setText("查询结果为：");
                db = mysqlhelper.getWritableDatabase();

                Cursor cursor = db.rawQuery("select * from user ",null );
                while (cursor.moveToNext()){
                    result.append("\n" +"________________________________________________________________________"+"\n"+
                            "标题：" + cursor.getString(1) +"\n" +
                            "内容：" + cursor.getString(2));
                }
                cursor.close();
                db.close();
                Toast.makeText(CollectActivity.this,"查询成功",Toast.LENGTH_LONG).show();
                Log.v("start","查询成功");
                break;
        }
    }

    /**
     * sqlite数据库创建帮助类
     */
    public class MyDBHelper extends SQLiteOpenHelper{

        public MyDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE user(" +
                    "user_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "username VARCHAR(2000),"+
                    "password VARCHAR(20000)"+
                    ")");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}