package com.example.register.activity;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.graphics.Color;

import com.example.register.R;
import com.lljjcoder.citypickerview.widget.CityPicker;

import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.register.dateoperation.Database;
public class registerActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText usernameEdit, passwordEdit, surePasswordEdit;
    private TextView cityText,regButton;
    private RadioGroup sexGroup;
    private RadioButton radioButton;
    Button register;
    EditText username, password;
    private CityPicker cityPicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        register = findViewById(R.id.register);
        username = findViewById(R.id.reg_username);
        password = findViewById(R.id.reg_password);

        register.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(this, userActivity.class);
            startActivity(intent);
        });
        init();

    }


    /**
      界面组件初始化
     */
    private void init() {
        cityText = findViewById(R.id.reg_province);
        cityText.setOnClickListener(this);
        usernameEdit = findViewById(R.id.reg_username);
        passwordEdit = findViewById(R.id.reg_password);
        surePasswordEdit = findViewById(R.id.reg_assure);
        regButton = findViewById(R.id.register);
        regButton.setOnClickListener(this);
        sexGroup = findViewById(R.id.sex);

        sexGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reg_province:
                initCityPicker();
                cityPicker.show();
                break;
            case R.id.register:
                //注册验证方法
                validateRegister();
                break;

        }
    }

    /**
     * 初始化城市选择器
     */
    public void initCityPicker() {
        cityPicker = new CityPicker.Builder(registerActivity.this)
                .textSize(16)//滚轮文字大小
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#EFB81C")
                .titleTextColor("#000000")
                .backgroundPop(0xa0000000)
                .confirTextColor("#000000")
                .cancelTextColor("#000000")
                .province("湖南省")
                .city("常德市")
                .district("武陵区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... strings) {
                String province = strings[0];
                String city = strings[1];
                String district = strings[2];
                cityText.setText(String.format("%s %s %s", province, city, district));
            }

            @Override
            public void onCancel() {
            }
        });

    }

    /**
     * 注册验证
     */
    public void validateRegister() {
        //Intent intent = new Intent(registerActivity.this, userActivity.class);
        String username = usernameEdit.getText().toString();
        String password = passwordEdit.getText().toString();
        String surePassword = surePasswordEdit.getText().toString();
        String sex = "";
        if(radioButton!=null){
            sex = radioButton.getText().toString();
            sex = sex == "男" ? "boy" : "girl";
        }else{
            Toast.makeText(registerActivity.this, "未选择性别", Toast.LENGTH_SHORT).show();
            return ;
        }
        String city = cityText.getText().toString();
        //判断两次密码是否输入一致
        if (password.equals(surePassword)) {
            //这里也可以再进行其它的验证，如是否符合要求等，符合就进行插入数据库操作
            if (!username.equals("") || !password.equals("")) {
                if (!city.equals("")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("username", username);
                    bundle.putString("password", password);
                    bundle.putString("sex", sex);
                    bundle.putString("city", city);

                    Database database = new Database(registerActivity.this);
                    SQLiteDatabase sqLiteDatabase = database.getWritableDatabase();
                    insertData(sqLiteDatabase, bundle);
                    Intent intent = new Intent(registerActivity.this, IndexActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(registerActivity.this, "请选择地址", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(registerActivity.this, "账号或密码未填写", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(registerActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
        }

    }


    /**
     * 插入数据库的值
     *
     * @param sqLiteDatabase
     * @param bundle
     */
    private void insertData(SQLiteDatabase sqLiteDatabase, Bundle bundle) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("username", bundle.getString("usernameT"));
        contentValues.put("password", bundle.getString("passwordT"));
        contentValues.put("sex", bundle.getString("sex"));
        contentValues.put("city", bundle.getString("city"));
        sqLiteDatabase.insert("meituan_user", null, contentValues);
        sqLiteDatabase.close();
    }
}



