package com.example.share2;


import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {


    private static final String ACTION_REGISTER_OK = "ACTION_REGISTER_OK";
    private  Boolean bPwdSwitch = false;
    private EditText etPwd;
    private EditText etCount;
    private Button btSignUp;
    private ConnectServer connectServer;
    private ResponseBody<ResponseData> responseBody;
    public class StartReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (responseBody != null){
                if(responseBody.getCode() == 200){
                    Toast.makeText(RegisterActivity.this,"注册成功！返回登录！",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(RegisterActivity.this,responseBody.getMsg(),Toast.LENGTH_SHORT).show();
                }

            }
            else {
                Toast.makeText(RegisterActivity.this,"服务器错误！",Toast.LENGTH_SHORT).show();
            }
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        this.getSupportActionBar().hide();


        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_REGISTER_OK);
        registerReceiver(new StartReceiver(), intentFilter);

        connectServer = new ConnectServer();
        etCount = findViewById(R.id.et_account);
        final ImageView ivPwdSwitch = findViewById(R.id.iv_pwd_switch);
        etPwd = findViewById(R.id.et_pwd);
        ivPwdSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                bPwdSwitch = !bPwdSwitch;
                if (bPwdSwitch) {
                    ivPwdSwitch.setImageResource(
                            R.drawable.ic_baseline_visibility_24);
                    etPwd.setInputType(
                            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ivPwdSwitch.setImageResource(
                            R.drawable.ic_baseline_visibility_off_24);
                    etPwd.setInputType(
                            InputType.TYPE_TEXT_VARIATION_PASSWORD |
                                    InputType.TYPE_CLASS_TEXT);
                    etPwd.setTypeface(Typeface.DEFAULT);
                }

            }
        });
        btSignUp = findViewById(R.id.bt_sign_up);
        btSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("conClick","点击了啊");

                HashMap<String,String> hMap = new HashMap<>();
                hMap.put("Content-Type", "application/json");
                Headers headers = connectServer.getHeaders(hMap);

                String username  = etCount.getText().toString();
                String password = etPwd.getText().toString();

                /*Json格式的body*/
                Map<String, String> body_map = new HashMap<>();
                body_map.put("username",username);
                body_map.put("password",password);
                RequestBody r_body  = connectServer.getJsonBody(body_map);


                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(Constants.SERVER_URL+Constants.USER_REGISTER)
                        .method("POST",r_body).headers(headers).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("request","请求失败了");
                        Intent musicStartIntent = new Intent(RegisterActivity.ACTION_REGISTER_OK);
                        sendBroadcast(musicStartIntent);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String body = response.body().string();
                        Gson gson = new Gson();
                        Type jsonType = new TypeToken<ResponseBody<ResponseData>>(){}.getType();
                        responseBody = gson.fromJson(body, jsonType);
                        Intent musicStartIntent = new Intent(RegisterActivity.ACTION_REGISTER_OK);
                        sendBroadcast(musicStartIntent);
                    }
                });
            }
        });
    }
}