package com.icaynia.soundki.View;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
;
import com.icaynia.soundki.Activity.MainActivity;
import com.icaynia.soundki.Data.PlayListManager;
import com.icaynia.soundki.Fragment.MyMusicListFragment;
import com.icaynia.soundki.Global;
import com.icaynia.soundki.Model.MusicList;
import com.icaynia.soundki.R;

import java.util.ArrayList;

/**
 * Created by icaynia on 14/03/2017.
 */

public class PlayListSelecter
{
    private Context context;
    private AdapterView.OnItemClickListener listener;
    private Bundle bundle;

    public PlayListSelecter(Context context)
    {
        this.context = context;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener)
    {
        this.listener = listener;
    }

    public void show()
    {
        showDialog(bundle);
    }

    public void showDialog(Bundle bundle) {
        final Activity activity = (Activity) context;
        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        Fragment prev = activity.getFragmentManager().findFragmentByTag("dialog2");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        //String inputText = inputTextField.getText().toString();
        ArrayList<String> list = (ArrayList<String>) bundle.get("adduid");
        MyDialogFragment newFragment = MyDialogFragment.newInstance(bundle);
        newFragment.setContext(context);
        newFragment.show(ft, "dialog2");

    }

    public void setBundle(Bundle bundle)
    {
        this.bundle = bundle;
    }

    public static class MyDialogFragment extends DialogFragment
    {
        private Context mContext;
        public void setContext(Context context)
        {
            mContext = context;
        }
        static MyDialogFragment newInstance(Bundle bundle) {
            MyDialogFragment f = new MyDialogFragment();
            f.setArguments(bundle);
            return f;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Global global = (Global) getActivity().getApplicationContext();
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_checkmenu, null, false);
            final ArrayList<String> adduid = (ArrayList<String>) getArguments().getStringArrayList("adduid");

            ArrayList<String> str = new ArrayList<>();

            PlayListManager plm = new PlayListManager(mContext);
            ArrayList<String> playlists = plm.getPlayListList();
            for (int i = 0; i < playlists.size(); i++)
            {
                str.add(playlists.get(i));
            }


            MenuListAdapter mla = new MenuListAdapter(mContext, str);
            ListView listViewt = (ListView) view.findViewById(R.id.listview);
            listViewt.setAdapter(mla);

            final AlertDialog.Builder builder  = new AlertDialog.Builder(getActivity())
                    .setIcon(R.mipmap.ic_launcher)
                    .setTitle("선택한 항목을 ..")
                    .setView(view)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    Toast.makeText(getActivity(), "OK", Toast.LENGTH_LONG).show();
                                }
                            }
                    );

            final AlertDialog dialog = builder.create();

            listViewt.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    switch (position)
                    {
                        // TODO add to next play
                        case 0:
                            break;

                        // TODO add to playlist
                        case 1:
                            break;

                        // TODO add to local playlist
                        case 2:
                            break;

                    }
                    Log.e("tag", position + " ");
                    dialog.dismiss();
                }
            });



            return dialog;
        }

    }


}

@@ -1650,74 +1650,8 @@
 );%0A%0A
-        //String inputText = inputTextField.getText().toString();%0A
     
@@ -1831,32 +1831,75 @@
 ntext(context);%0A
+        newFragment.setListener(listener);%0A
         newFragm
@@ -2111,32 +2111,90 @@
 ntext mContext;%0A
+        private AdapterView.OnItemClickListener listener;%0A
         public v
@@ -2269,32 +2269,166 @@
 text;%0A        %7D%0A
+%0A        public void setListener(AdapterView.OnItemClickListener listener)%0A        %7B%0A            this.listener = listener;%0A        %7D%0A%0A
         static M
@@ -3253,24 +3253,70 @@
 yListList();
+%0A%0A            //str.add(%22%EC%83%88%EB%A1%9C%EC%9A%B4 %ED%94%8C%EB%A0%88%EC%9D%B4%EB%A6%AC%EC%8A%A4%ED%8A%B8%EC%97%90 %EC%A0%80%EC%9E%A5 ...%22);
 %0A           
@@ -3707,16 +3707,17 @@
 ivity())
+;
 %0A       
@@ -3717,32 +3717,31 @@
             
-        
+builder
 .setIcon(R.m
@@ -3758,21 +3758,18 @@
 auncher)
+;
 %0A
-    
         
@@ -3768,28 +3768,31 @@
             
-    
+builder
 .setTitle(%22%EC%84%A0
@@ -3802,16 +3802,17 @@
 %ED%95%AD%EB%AA%A9%EC%9D%84 ..%22)
+;
 %0A       
@@ -3812,32 +3812,31 @@
             
-        
+builder
 .setView(vie
@@ -3837,16 +3837,17 @@
 ew(view)
+;
 %0A       
@@ -3851,24 +3851,23 @@
         
-        
+builder
 .setPosi
@@ -3886,36 +3886,8 @@
 OK%22,
-%0A                           
  new
@@ -3939,32 +3939,16 @@
         
-             
-   
 public v
@@ -4022,32 +4022,16 @@
         
-        
-        
 Toast.ma
@@ -4105,76 +4105,23 @@
     
-  
-              %7D%0A                            %7D%0A                    
+%7D%0A            %7D
 );%0A%0A
@@ -4260,28 +4260,16 @@
 stener()
-%0A           
  %7B%0A     
@@ -4428,510 +4428,95 @@
     
-switch (position)%0A                    %7B%0A                        // TODO add to next play%0A                        case 0:%0A                            break;%0A%0A                        // TODO add to playlist%0A                        case 1:%0A                            break;%0A%0A                        // TODO add to local playlist%0A                        case 2:%0A                            break;%0A%0A                    %7D%0A                    Log.e(%22tag%22, position + %22 %22);%0A                    dialog.dismiss(
+// for customizing.%0A                    listener.onItemClick(parent, view, position, id
 );%0A 

Refactor the code