package com.pitchedapps.material.glass.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.melnykov.fab.FloatingActionButton;
import com.pitchedapps.material.glass.R;
import com.pitchedapps.material.glass.adapters.RequestAdapter;
import com.pkmmte.requestmanager.AppInfo;
import com.pkmmte.requestmanager.PkRequestManager;

import java.util.LinkedList;
import java.util.List;

public class RequestFragment extends Fragment {

    // App List
    private List<AppInfo> mApps = new LinkedList<>();
    // Request Manager
    private PkRequestManager mRequestManager;
    // List & Adapter
    private ListView mList;
    private RequestAdapter mAdapter;
//    private ListAdapter mAdapter;
    private View mProgress;
    private FloatingActionButton fab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.section_icon_request, container, false);

//        showNewAdviceDialog();

        ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (toolbar != null)
            toolbar.setTitle(R.string.section_five);

        // Populate your ListView with your apps
        mList = (ListView) root.findViewById(R.id.appList);
        mList.setVisibility(View.GONE);

        // Progress
        mProgress = root.findViewById(R.id.progress);

        new GrabApplicationsTask().execute();

        // Set basic listener to your ListView
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Mark the app as selected
                AppInfo mApp = mApps.get(position);
                mApp.setSelected(!mApp.isSelected());
                mApps.set(position, mApp);

                // Let the adapter know you selected something
                mAdapter.notifyDataSetChanged();
            }
        });

        fab = (FloatingActionButton) root.findViewById(R.id.send_btn);
        fab.hide(true);
        fab.attachToListView(mList);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ProgressDialog dialog;
//                dialog = new ProgressDialog(getActivity());
//                dialog.setMessage("Your message..");
//                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//                dialog.setMax(100);
//                dialog.setCanceledOnTouchOutside(true);
//                dialog.show();
                Toast.makeText(getActivity(), getString(R.string.building_request), Toast.LENGTH_LONG).show();
                mRequestManager.setActivity(getActivity());
                if (mRequestManager.getNumSelected() < 1)
                    mRequestManager.sendAutomaticRequestAsync();
                else
                    mRequestManager.sendRequestAsync();
//                dialog.dismiss();
            }

        });

        fab.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(getActivity(), getString(R.string.unfiltered_request), Toast.LENGTH_LONG).show();
                mRequestManager.removeAllAppFilterListeners();
//                mRequestManager.setActivity(getActivity());
//                if (mRequestManager.getNumSelected() < 1)
//                    mRequestManager.sendRequest(true, false);
                //TODO check if this is right
                return false;
            }
        });
        return root;
    }

    private class GrabApplicationsTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            try {
                mRequestManager = PkRequestManager.getInstance(getActivity());
                mRequestManager.setDebugging(false);
                mRequestManager.loadAppsIfEmpty();
                // Get the list of apps
//                mApps.addAll(mRequestManager.getApps());
                //TODO fix this horrrible loading
                mApps = mRequestManager.getApps();
//                mApps.add(mRequestManager.getApps());
            } catch (Exception ex) {
                //could happen that the activity detaches :D
            }
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            mAdapter = new RequestAdapter(getActivity(), mApps);
//            mAdapter = new ListAdapter(mApps);
            mList.setAdapter(mAdapter);
            if (mAdapter != null)
                mAdapter.notifyDataSetChanged();
            if (mList != null)
                mList.setVisibility(View.VISIBLE);
            if (fab != null)
                fab.show(true);
            if (mProgress != null)
                mProgress.setVisibility(View.GONE);
        }
    }

    // You should probably put this in a separate .java file
    /*private class ListAdapter extends BaseAdapter {

        private final List<AppInfo> mApps;

        public ListAdapter(List<AppInfo> apps) {
            this.mApps = apps;
        }

        @Override
        public int getCount() {
            return mApps.size();
        }

        @Override
        public AppInfo getItem(int position) {
            return mApps.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            AppInfo mApp = mApps.get(position);

            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(getActivity());
                convertView = inflater.inflate(R.layout.request_item, parent, false);

                holder = new ViewHolder();
                holder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
                holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
                holder.chkSelected = (CheckBox) convertView.findViewById(R.id.chkSelected);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtName.setText(mApp.getName());
            holder.imgIcon.setImageDrawable(mApp.getImage());
            holder.chkSelected.setChecked(mApp.isSelected());

            return convertView;
        }

        private class ViewHolder {
            public ImageView imgIcon;
            public TextView txtName;
            public CheckBox chkSelected;
        }
    }*/
}
@@ -1026,17 +1026,39 @@
 Adapter;
+ (now request adapter)
 %0A
-
     priv
@@ -2646,16 +2646,60 @@
 ew v) %7B%0A
+                //TODO add progress dialog?%0A
 //      
@@ -4623,16 +4623,72 @@
  loading
+ (creates diplicates; could be a small error on my side)
 %0A       
@@ -5486,1793 +5486,11 @@
   %7D%0A
+
     %7D
-%0A%0A    // You should probably put this in a separate .java file%0A    /*private class ListAdapter extends BaseAdapter %7B%0A%0A        private final List%3CAppInfo%3E mApps;%0A%0A        public ListAdapter(List%3CAppInfo%3E apps) %7B%0A            this.mApps = apps;%0A        %7D%0A%0A        @Override%0A        public int getCount() %7B%0A            return mApps.size();%0A        %7D%0A%0A        @Override%0A        public AppInfo getItem(int position) %7B%0A            return mApps.get(position);%0A        %7D%0A%0A        @Override%0A        public long getItemId(int position) %7B%0A            return position;%0A        %7D%0A%0A        @Override%0A        public View getView(int position, View convertView, ViewGroup parent) %7B%0A            ViewHolder holder;%0A            AppInfo mApp = mApps.get(position);%0A%0A            if (convertView == null) %7B%0A                LayoutInflater inflater = LayoutInflater.from(getActivity());%0A                convertView = inflater.inflate(R.layout.request_item, parent, false);%0A%0A                holder = new ViewHolder();%0A                holder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);%0A                holder.txtName = (TextView) convertView.findViewById(R.id.txtName);%0A                holder.chkSelected = (CheckBox) convertView.findViewById(R.id.chkSelected);%0A%0A                convertView.setTag(holder);%0A            %7D else %7B%0A                holder = (ViewHolder) convertView.getTag();%0A            %7D%0A%0A            holder.txtName.setText(mApp.getName());%0A            holder.imgIcon.setImageDrawable(mApp.getImage());%0A            holder.chkSelected.setChecked(mApp.isSelected());%0A%0A            return convertView;%0A        %7D%0A%0A        private class ViewHolder %7B%0A            public ImageView imgIcon;%0A            public TextView txtName;%0A            public CheckBox chkSelected;%0A        %7D%0A    %7D*/
 %0A%7D

clean up