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.LoginFragmentConnection;
import org.ntlab.radishforandroidstudio.cactusClient.models.URIAddressedAccount;
import org.ntlab.radishforandroidstudio.framework.network.CallBack;


public class LoginFragment extends Fragment {
    private LoginFragmentConnection connection;

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

    @Override
    public void onStart() {
        super.onStart();

        Button button = (Button)getActivity().findViewById(R.id.signUP1);
        //↓↓↓↓↓↓↓↓画面遷移↓↓↓↓↓↓↓↓
        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 SignUpFragment());
                transaction.commit();
            }
        });
        //↑↑↑↑↑画面遷移↑↑↑↑↑↑

        Button loginButton = (Button)getActivity().findViewById(R.id.login);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText userName = (EditText)getActivity().findViewById(R.id.inputUserName);
                EditText passWord = (EditText)getActivity().findViewById(R.id.checkPassword);

//                /****************************************************************
//                テストするなら以下を実行するとパスの入力をしなくても画面遷移します
                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();

                transaction.replace(R.id.fragment_place, new InstancesFragment());

                transaction.commit();
//                 **********************************************************************/

/*
                if (userName.getText().toString().isEmpty()) {
                    Toast.makeText(getActivity(), "ユーザー名が未記入です", Toast.LENGTH_SHORT).show();
                    return;
                } else if(passWord.getText().toString().isEmpty()){
                    Toast.makeText(getActivity(), "パスワードが未記入です", Toast.LENGTH_SHORT).show();
                    return;
                }

                connection = new LoginFragmentConnection();
                connection.setCallBack(new CallBack() {
                    @Override
                    public void onResponse(String response) {
                        //通信した時の処理を書くのだよ
                        URIAddressedAccount lac = JSON.decode(response, new TypeReference<URIAddressedAccount>(){});
                        System.out.println("///////////////////////////////////////////////");
                        System.out.println(response);

                        if(response.contains("\"login\":true,")){
                            Toast.makeText(getActivity(),"ログインに成功しました。", Toast.LENGTH_SHORT).show();
                            FragmentManager manager = getFragmentManager();
                            FragmentTransaction transaction = manager.beginTransaction();
                            transaction.replace(R.id.fragment_place, new InstancesFragment());
                            transaction.commit();
                        }else {
                            Toast.makeText(getActivity(),"ログインに失敗しました。", Toast.LENGTH_SHORT).show();
                        }

                    }
                });
                connection.addFormParam("userID", userName.getText().toString());
                connection.addFormParam("userPass", passWord.getText().toString());
                connection.doPut();
                */

            }
        });


    }
}
