package ca.etsmtl.applets.etsmobile.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.octo.android.robospice.persistence.exception.SpiceException;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import ca.etsmtl.applets.etsmobile.db.DatabaseHelper;
import ca.etsmtl.applets.etsmobile.http.AppletsApiSponsorRequest;
import ca.etsmtl.applets.etsmobile.model.Sponsor;
import ca.etsmtl.applets.etsmobile.ui.adapter.SponsorAdapter;
import ca.etsmtl.applets.etsmobile.util.SponsorManager;
import ca.etsmtl.applets.etsmobile.views.LoadingView;
import ca.etsmtl.applets.etsmobile2.R;

public class SponsorsFragment extends HttpFragment implements Observer {

    private GridView sponsorGridView;
    private SponsorAdapter adapter;
    private ArrayList<Sponsor> sponsorList;
    private SponsorManager mSponsorManager;
    private LoadingView loadingView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup v = (ViewGroup) inflater.inflate(R.layout.fragment_sponsors, container, false);
        loadingView = (LoadingView) v.findViewById(R.id.loadingView_sponsor);
        sponsorGridView = (GridView) v.findViewById(R.id.gridView_sponsor);
        sponsorList = new ArrayList<Sponsor>();
        mSponsorManager = new SponsorManager(getActivity());
        mSponsorManager.addObserver(this);
        loadingView.showLoadingView();
        refreshList();
        dataManager.sendRequest(new AppletsApiSponsorRequest(getActivity()), this);


        return v;
    }

    @Override
    public void onRequestFailure(SpiceException e) {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LoadingView.hideLoadingView(loadingView);
                }
            });
        }
        Toast.makeText(getActivity(), getString(R.string.toast_Sync_Fail), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestSuccess(Object o) {
        super.onRequestSuccess(o);
        mSponsorManager.onRequestSuccess(o);
        if (loadingView != null && getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LoadingView.hideLoadingView(loadingView);
                }
            });
        }
    }

    public void refreshList() {

        sponsorList = mSponsorManager.getSponsorList();
        adapter = new SponsorAdapter(getActivity(), R.layout.row_sponsor, sponsorList, this);
        sponsorGridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        sponsorGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Sponsor item = sponsorList.get(position);
                String url = item.getUrl();
                Intent internetIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(url));
                startActivity(internetIntent);
            }
        });
    }

    @Override
    void updateUI() {
    }

    @Override
    public void update(Observable observable, Object o) {
        refreshList();
    }
}

@@ -3455,24 +3455,79 @@
 m.getUrl();%0A
+                if (url != null)%0A                %7B%0A    
             
@@ -3609,16 +3609,20 @@
         
+    
 Uri.pars
@@ -3646,16 +3646,20 @@
         
+  
+  
 startAct
@@ -3681,16 +3681,34 @@
 ntent);%0A
+                %7D%0A
         

Fix bug when null sponsor URL