package com.usai.apex.mainframe;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.usai.apex.ChangePasswordActivity;
import com.usai.apex.R;
import com.usai.apex.saved.CheckSavedActivity;

import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends StaticModelistFragment {

    static final int PASSWORD_CHANGED = 1;
    private static final int	REQUEST_CHANGEPASSWORD_ACTIVITY	= 2;


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {

//			case REQUEST_LOGINACTIVITY:
//				if (resultCode == Activity.RESULT_CANCELED)
//					getActivity().finish();
//				break;
            case REQUEST_CHANGEPASSWORD_ACTIVITY:
                if (resultCode == Activity.RESULT_OK)
                {
//					String UNIQUE_STRING = "com.usai.apex.push.cancel";
//					Intent intent = new Intent(UNIQUE_STRING);
//					getActivity().sendBroadcast(intent);
//					Intent i = new Intent();
//					i.setClass(getActivity(), ApexActivity.class);
//					startActivityForResult(i, REQUEST_LOGINACTIVITY);
                    getActivity().setResult(PASSWORD_CHANGED, null);
                    getActivity().finish();
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    public interface LogoutCallBack{
        //        public abstract void onLogin();
        public abstract void onLogout();
    }
    private LogoutCallBack mCallBack;
    public void setCallBack(LogoutCallBack callBack) {
        this.mCallBack = callBack;
    }

    public MyFragment() {
        // Required empty public constructor
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        setupAdapter(R.raw.my);

//        adapter.getView()

        customize_style();
//        JSONObject jsonobj=loadjson(R.raw.search);
//        adapter        = new SimpleRoundCornerAdapter(this.getActivity(),
//                getData(jsonobj), R.layout.static_modelist_cell, new String[] { "title",
//                "img","detail" }, new int[] { R.id.tv_name, R.id.iv_icon,R.id.tv_detail });
//        setListAdapter(adapter);
////        this.getListView().setDivider(R.drawable.);
//        this.getListView().setDividerHeight(18);
//        this.getListView().setBackgroundColor(Color.WHITE);

    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id)
    {

        JSONObject jsonobj=loadjson(R.raw.my);
        Log.d("FragmentList", "Item clicked: " + id);

        String 	title = (String) getData(jsonobj).get(position-1).get("title");

//        Intent intent = new Intent();
        if (title.equals("Saved Detail"))
        {
            Intent intent = new Intent();
            intent.setClass(getActivity(), CheckSavedActivity.class);
            // intent.putExtra("user", ApexTrackingApplication.get_user());
            // intent.putExtra("password", password);
            intent.putExtra("function_name", "Saved Detail");
//            intent.putExtra("title", "Booking Search");
            startActivity(intent);
        }
        else if (title.equals("Saved Search"))
        {
            Intent intent = new Intent();
            intent.setClass(getActivity(), CheckSavedActivity.class);
            // intent.putExtra("user", user);
            // intent.putExtra("password", password);
            intent.putExtra("function_name", "Saved Search");
//            intent.putExtra("title", "B/L info. Search");
            startActivity(intent);
        }

        else if(title.equals("Saved Documents"))
        {
//            Intent intent = new Intent();
//            intent.setClass(getActivity(), LocalDocumentActivity.class);
////			intent.putExtra("module_name", "Announcements");
//            startActivity(intent);

            Intent intent = new Intent();
            intent.setClass(getActivity(), CheckSavedActivity.class);
            intent.putExtra("function_name", "Saved Documents");

            startActivity(intent);

        }
        else if (title.equals("Change Password"))
        {
            Intent intent = new Intent();
			intent.setClass(getActivity(), ChangePasswordActivity.class);

			startActivityForResult(intent, REQUEST_CHANGEPASSWORD_ACTIVITY);
			return;
        }
        else if (title.equals("Logout"))
        {

            String UNIQUE_STRING = "com.usai.apex.push.cancel";
            Intent intent = new Intent(UNIQUE_STRING);
            // intent.putExtra("key1", "value1");
            // intent.putExtra("key2", "value2");
            getActivity().sendBroadcast(intent);
            if(mCallBack!=null)
                mCallBack.onLogout();

            {

                Intent bintent = new Intent("Login");
                bintent.putExtra("state", false);
                LocalBroadcastManager.getInstance(getActivity())
                        .sendBroadcast(bintent);
            }

        }


    }

}
