package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.style.citylist.utils.CityListLoader;
import com.lljjcoder.style.citythreelist.CityBean;
import com.lljjcoder.style.citythreelist.ProvinceActivity;

public class RegisterActivity extends AppCompatActivity implements
    View.OnClickListener,RadioGroup.OnCheckedChangeListener{

    private EditText edt_name;
    private EditText edt_password;
    private EditText edt_surePassword;
    private Button regButton;
    private RadioGroup sexGroup;
    private TextView cityText;
    private String sexStr="男";

    CityBean  province, city,area;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_login);
        init();
        CityListLoader.getInstance().loadProData(this);
        findViewById(R.id.reg_province).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this, ProvinceActivity.class);

                startActivityForResult(intent, ProvinceActivity.RESULT_DATA);

            }
        });


    }
    public void init(){
        cityText =findViewById(R.id.reg_province);
        cityText.setOnClickListener(this);
        edt_name=findViewById(R.id.reg_username);
        edt_password=findViewById(R.id.reg_password);
        edt_surePassword=findViewById(R.id.sure_password);
        regButton=findViewById(R.id.reg_login);
        regButton.setOnClickListener(this);
        sexGroup=findViewById(R.id.sex);
        sexGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ProvinceActivity.RESULT_DATA) {
            if (resultCode == RESULT_OK) {
                if (data == null) {
                    return;
                }
                //省份结果
                province = data.getParcelableExtra("province");
                //城市结果
                city = data.getParcelableExtra("city");
                //区域结果
                area = data.getParcelableExtra("area");

                TextView textView = findViewById(R.id.reg_province);
                textView.setText(String.format("%s %s %s", province.getName(), city.getName(), area.getName()));
            }
        }
    }
   public void onClick(View view){
            Intent intent = new Intent(RegisterActivity.this, UserActivity.class);
            String username = edt_name.getText().toString();
            String password = edt_password.getText().toString();
            String surePassword = edt_surePassword.getText().toString();
            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", sexStr);
                        bundle.putString("city", city);
                        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();
            }
   }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup,int checkedId) {
        sexStr=checkedId==R.id.man?"男":"女";
    }
}