package org.ntlab.radishforandroidstudio.cactusClient.views;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import net.arnx.jsonic.JSON;
import net.arnx.jsonic.TypeReference;

import org.ntlab.radishforandroidstudio.R;
import org.ntlab.radishforandroidstudio.cactusClient.connections.SignUpFragmentConnection;
import org.ntlab.radishforandroidstudio.cactusClient.models.URIAddressedAccount;
import org.ntlab.radishforandroidstudio.framework.network.CallBack;

public class SignUpFragment extends Fragment {
    private SignUpFragmentConnection connection;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.sign_up_fragment, container, false);
    }

    //↓↓↓↓↓↓↓↓画面遷移↓↓↓↓↓↓↓↓
    @Override
    public void onStart() {

        super.onStart();

        //ログインボタン
        Button button = (Button) getActivity().findViewById(R.id.LoginButton2);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity(), "hoge!", Toast.LENGTH_SHORT).show();

                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();

                transaction.addToBackStack(null);
                //transaction.replace(R.id.fragment_place, new LoginFragment());
                transaction.replace(R.id.fragment_place, new LoginFragment());

                transaction.commit();
            }
        });

        //サインアップ
        Button signUpButton = (Button) getActivity().findViewById(R.id.signUp2);
        signUpButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                EditText inputPassword = (EditText)getActivity().findViewById(R.id.inputPassword2);
                EditText checkPassWord = (EditText)getActivity().findViewById(R.id.checkPassword);
                EditText inputUserName = (EditText)getActivity().findViewById(R.id.inputUserName);
                EditText inputUserID = (EditText)getActivity().findViewById(R.id.inputUserID);


                if(inputUserID.getText().toString().contains(" ") || inputUserID.getText().toString().contains("　")){
                    Toast.makeText(getActivity(), "空白の含むIDは登録できません", Toast.LENGTH_SHORT).show();
                    return;
                }else if (inputPassword.getText().toString().isEmpty()) {
                    Toast.makeText(getActivity(), "パスワードを入力してください", Toast.LENGTH_SHORT).show();
                    return;
                } else if (checkPassWord.getText().toString().isEmpty()) {
                    Toast.makeText(getActivity(), "確認用パスワードを入力してください", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!inputPassword.getText().toString().equals(checkPassWord.getText().toString() )) {
                    Toast.makeText(getActivity(), "パスワードが一致しません", Toast.LENGTH_SHORT).show();
                    return;
                }

                connection = new SignUpFragmentConnection();
                connection.setCallBack(new CallBack() {
                    @Override
                    public void onResponse(String response) {
                        //通信した時の処理を書くのだよ
                        URIAddressedAccount ac = JSON.decode(response, URIAddressedAccount.class);
                        URIAddressedAccount ac2 = JSON.decode(response, new TypeReference<URIAddressedAccount>(){});
                        System.out.println("JSON.decode(response, URIAddressedAccount.class);\n"+ac.getUri());
                        System.out.println("JSON.decode(response, new TypeReference<URIAddressedAccount>(){});\n"+ac2.getUri());
                        System.out.println(response);
                        Toast.makeText(getActivity(),response, Toast.LENGTH_SHORT).show();
                    }
                });

                connection.addFormParam("userID", inputUserID.getText().toString());
                connection.addFormParam("userPass", inputPassword.getText().toString());
                connection.addFormParam("userName", inputUserName.getText().toString());
                connection.doPost();


            }
        });
    }


}