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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


import net.arnx.jsonic.JSON;
import net.arnx.jsonic.TypeReference;

import org.ntlab.radishforandroidstudio.R;
import org.ntlab.radishforandroidstudio.cactusClient.Cactus;
import org.ntlab.radishforandroidstudio.cactusClient.connections.InstancesFragmentConnection;
import org.ntlab.radishforandroidstudio.cactusClient.connections.SignUpFragmentConnection;
import org.ntlab.radishforandroidstudio.cactusClient.models.Account;
import org.ntlab.radishforandroidstudio.cactusClient.models.Instance;
import org.ntlab.radishforandroidstudio.cactusClient.models.URIAddressedAccount;
import org.ntlab.radishforandroidstudio.framework.network.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstancesFragment extends Fragment {
    private InstancesFragmentConnection connection;
    ArrayList<Map.Entry<String,Instance>> listInstances ;
    ListView listView;

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        connection = new InstancesFragmentConnection();
        connection.setCallBack(new CallBack() {
            @Override
            public void onResponse(String response) {
                //通信した時の処理を書くのだよ
                HashMap<String,Instance> mapInstances = JSON.decode(response, new TypeReference<HashMap<String,Instance>>(){});
                System.out.println(response);
//                for(Map.Entry<String,Instance> entry : mapInstances.entrySet()){
//                    listInstances.add(entry);
//                }
                listInstances = new ArrayList<>(mapInstances.entrySet());
                ArrayList<String> listNames = new ArrayList<>();
                for(Map.Entry<String,Instance> entry : listInstances) {
                    listNames.add(entry.getValue().getName());
                }
                // ListViewをセット
                ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, listNames);
                listView = (ListView) view.findViewById(R.id.list_view);
                listView.setAdapter(adapter);

                // セルを選択されたら詳細画面フラグメント呼び出す
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View v, int InstanceNum, long id) {

                        //ここでサーバーに何番のインスタンスが選ばれたのかを送信して、受け取った値に応じてキャラクターを送信しなければならない
                        //...................................................
                        //InstanceNum+で選ばれたインスタンスわかるで！数字は０からやで！例えば１はデータ２やで！やで
                        String[] s = listInstances.get(InstanceNum).getKey().split("/");

                        FragmentManager manager = getFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        CharactersFragment fragment = new CharactersFragment();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("instanceId",s[s.length-1]);
                        fragment.setArguments(bundle);
                        transaction.replace(R.id.fragment_place, fragment,"characters_fragment");
                        transaction.addToBackStack(null);
                        transaction.commit();
                    }
                });
            }
        });
        connection.doGet();
    }

}