package athary.audio.ency;


import java.util.ArrayList;
import java.util.TreeSet;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import athary.audio.ency.Helpers.data.NewDataDbAdapter;
import athary.audio.ency.Helpers.main.FileChooser;

public class Settings_Activity extends Activity{
	
//Define Views
private ListView list;

//Define Other Variables
private String path;
private int PlayStop;
private int FontType;
private int FontSize;
private int linkNo;
private String searchwords;
private SharedPreferences athary;
private SharedPreferences.Editor atharyEditor;
private MyCustomAdapter mAdapter;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = 7;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private TreeSet mSeparatorsSet = new TreeSet();
private String Save_Path;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
	
        initSharedPreferences();
    	initViews();
    	fillData();

	}
	
	private void initSharedPreferences() {

		athary = getSharedPreferences("athary", 0);

        path = athary.getString("path", "");
        linkNo = athary.getInt("linkNo", 1);
        if (linkNo == 3) {
        	   linkNo = 1;
			   atharyEditor = athary.edit();
			   atharyEditor.putString("link", "http://www.maknoon.com/download/audios/");
			   atharyEditor.putInt("linkNo", 1);
			   atharyEditor.commit();	
        }
        PlayStop = athary.getInt("PlayStop", 1);
        FontType = athary.getInt("FontType", 3);
        FontSize = athary.getInt("FontSize", 1);
        searchwords = athary.getString("TERMS", "");
		Save_Path = athary.getString("SAVE", "");           

	}

	private void initViews() {

        list = (ListView) findViewById(R.id.list);
		
	}

	private void fillData() {

        mAdapter = new MyCustomAdapter();
        mAdapter.addItem("مسار قاعدة البيانات");
        mAdapter.addItem("مصدر الملفات الصوتية");
        mAdapter.addItem("مسار الصوتيات المحملة");
        mAdapter.addItem("طريقة الاستماع للفهارس");
        mAdapter.addItem("كلمات البحث");	                       
        mAdapter.addItem("نوع الخط (يجب إعادة تشغيل البرنامج)");	 
        mAdapter.addItem("حجم الخط  (يجب إعادة تشغيل البرنامج)");	                       

    list.setAdapter(mAdapter);
		
	}
 
    private class MyCustomAdapter extends BaseAdapter {
 

 
        public MyCustomAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
 
        public void addItem(final String item) {
            mData.add(item);
            notifyDataSetChanged();
        }
 
        public void addSeparatorItem(final String item) {
            mData.add(item);
            // save separator position
            mSeparatorsSet.add(mData.size() - 1);
            notifyDataSetChanged();
        }
 
        @Override
        public int getItemViewType(int position) {
            return position;
        }
 
        @Override
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }
 
        @Override
        public int getCount() {
            return mData.size();
        }
 
        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = position;
            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                    case 0:
                        convertView = mInflater.inflate(R.layout.settings_database, null);
                        holder.textView = (TextView) convertView.findViewById(R.id.title);
                        holder.datapath = (TextView) convertView.findViewById(R.id.path);
                        holder.btnpath = (Button) convertView.findViewById(R.id.btnpath);
    		            holder.btnpath.setTypeface(Typeface.createFromAsset(getAssets(), "font2.ttf"),Typeface.NORMAL);
    		            holder.btnpath.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);

                        holder.datapath.setText(path);
                        holder.btnpath.setOnClickListener(new View.OnClickListener() {
							
							@Override
							public void onClick(View v) {
								Intent i = new Intent(getApplicationContext(), Wizard.class);
								startActivity(i);									}
						});
                        
                        break;
                        
                    case 1:
                        convertView = mInflater.inflate(R.layout.settings_rows, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.title);
                        holder.spinners = (Spinner)convertView.findViewById(R.id.spin);
                    	String [] spinnerdata = new String[]{"موقع مكنون","موقع أهل الحديث والأثر"};
                    	SettingsSpinnerAdapter typeadapter = new SettingsSpinnerAdapter(Settings_Activity.this, spinnerdata);
                    	holder.spinners.setAdapter( typeadapter );
                    	holder.spinners.setSelection(linkNo - 1);

                    	holder.spinners.setOnItemSelectedListener(new OnItemSelectedListener() {

                    		@Override
                    		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    				long arg3) {

                    			switch (arg2) {
								case 0:
									   atharyEditor = athary.edit();
									   atharyEditor.putString("link", "http://www.maknoon.com/download/audios/");
									   atharyEditor.putInt("linkNo", 1);
									   atharyEditor.commit();	
									break;
								case 1:
									   atharyEditor = athary.edit();
									   atharyEditor.putString("link", "http://www.alathar.net/files/sound/");
									   atharyEditor.putInt("linkNo", 2);
									   atharyEditor.commit();	
									break;
								default:
									break;
								}
                    		}

                    		@Override
                    		public void onNothingSelected(AdapterView<?> arg0) {
                    			// TODO Auto-generated method stub
                    			
                    		}
                    	});
                        break;
                       
                case 2:
                    convertView = mInflater.inflate(R.layout.settings_save_path, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.title);
                    holder.datapath = (TextView) convertView.findViewById(R.id.path);
                    holder.btnpath = (Button) convertView.findViewById(R.id.btnpath);
		            holder.btnpath.setTypeface(Typeface.createFromAsset(getAssets(), "font2.ttf"),Typeface.NORMAL);
		            holder.btnpath.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);

                    holder.datapath.setText(Save_Path);
                    holder.btnpath.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							
								Intent i = new Intent(getApplicationContext(), FileChooser.class);
								i.putExtra("FLAG", "DOWNLOAD");
								startActivityForResult(i, 0);
						}
					});
                    break;

                    case 3:
                        convertView = mInflater.inflate(R.layout.settings_rows, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.title);
                        holder.spinners = (Spinner)convertView.findViewById(R.id.spin);
                    	String [] playmodes = new String[]{"الاستماع المتواصل","التوقف عند نهاية الفهرس"};
                    	SettingsSpinnerAdapter playmodesadapter = new SettingsSpinnerAdapter(Settings_Activity.this, playmodes);
                    	holder.spinners.setAdapter( playmodesadapter );
                    	holder.spinners.setSelection(PlayStop - 1);

                    	holder.spinners.setOnItemSelectedListener(new OnItemSelectedListener() {

                    		@Override
                    		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    				long arg3) {

                    			switch (arg2) {
								case 0:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("PlayStop", 1);
									   atharyEditor.commit();	
									break;
								case 1:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("PlayStop", 2);
									   atharyEditor.commit();	
									break;

								default:
									break;
								}
                    		}

                    		@Override
                    		public void onNothingSelected(AdapterView<?> arg0) {
                    			// TODO Auto-generated method stub
                    			
                    		}
                    	});
                        break;
                        
                    case 4:
                        convertView = mInflater.inflate(R.layout.settings_search_clear, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.title);
                        holder.btnclearsearch = (Button) convertView.findViewById(R.id.btnclearsearch);
                        
    		            holder.btnclearsearch.setTypeface(Typeface.createFromAsset(getAssets(), "font2.ttf"),Typeface.NORMAL);
    		            holder.btnclearsearch.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);

                        if (searchwords.equals("")){
                        	
                        	holder.btnclearsearch.setEnabled(false);
                        	
                        }else if (!searchwords.equals("")){
                        	
                        	holder.btnclearsearch.setEnabled(true);

                        }
                        
                        holder.btnclearsearch.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View arg0) {

								atharyEditor = athary.edit();
								atharyEditor.putString("TERMS", "");
								atharyEditor.commit();
								   
								   arg0.setEnabled(false);

							}
						});
                        break;
                     
                    case 5:
                        convertView = mInflater.inflate(R.layout.settings_rows, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.title);
                        holder.spinners = (Spinner)convertView.findViewById(R.id.spin);
                    	String [] fonts = new String[]{"الخط الافتراضي", "خط المهند", "الخط الديواني", "خط الجزيرة"};
                    	SettingsSpinnerAdapter fontsadapter = new SettingsSpinnerAdapter(Settings_Activity.this, fonts);
                    	holder.spinners.setAdapter( fontsadapter );
                    	holder.spinners.setSelection(FontType - 1);

                    	holder.spinners.setOnItemSelectedListener(new OnItemSelectedListener() {

                    		@Override
                    		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    				long arg3) {

                    			switch (arg2) {
								case 0:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontType", 1);
									   atharyEditor.commit();	
									break;
								case 1:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontType", 2);
									   atharyEditor.commit();	
									break;
								case 2:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontType", 3);
									   atharyEditor.commit();	
									break;
								case 3:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontType", 4);
									   atharyEditor.commit();	
									break;

								default:
									break;
								}
                    		}

                    		@Override
                    		public void onNothingSelected(AdapterView<?> arg0) {
                    			// TODO Auto-generated method stub
                    			
                    		}
                    	});
                    	break;
  
                    case 6:
                        convertView = mInflater.inflate(R.layout.settings_rows, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.title);
                        holder.spinners = (Spinner)convertView.findViewById(R.id.spin);
                    	String [] fontsize = new String[]{"خط أصغر", "خط صغير", "خط متوسط", "خط كبير", "خط أكبر"};
                    	SettingsSpinnerAdapter fontsizeadapter = new SettingsSpinnerAdapter(Settings_Activity.this, fontsize);
                    	holder.spinners.setAdapter( fontsizeadapter );
                    	holder.spinners.setSelection(FontSize - 1);

                    	holder.spinners.setOnItemSelectedListener(new OnItemSelectedListener() {

                    		@Override
                    		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    				long arg3) {

                    			switch (arg2) {
								case 0:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontSize", 1);
									   atharyEditor.commit();	
									break;
								case 1:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontSize", 2);
									   atharyEditor.commit();	
									break;
								case 2:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontSize", 3);
									   atharyEditor.commit();	
									break;
								case 3:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontSize", 4);
									   atharyEditor.commit();
									break;
								case 4:
									   atharyEditor = athary.edit();
									   atharyEditor.putInt("FontSize", 5);
									   atharyEditor.commit();	
									break;
									
								default:
									break;
								}
                    		}

                    		@Override
                    		public void onNothingSelected(AdapterView<?> arg0) {
                    			// TODO Auto-generated method stub
                    			
                    		}
                    	});
                        break;

                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.textView.setText(mData.get(position).toString());
            holder.textView.setTypeface(Typeface.createFromAsset(getAssets(), "font2.ttf"),Typeface.NORMAL);
            holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);

            return convertView;
        }
 
    }
 
    public static class ViewHolder {
    	
        public Spinner spinners;
        public TextView textView;
        
        public TextView datapath;
        public Button btnpath;
        public Button btnclearsearch;

    }
    
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

	     String extraData=data.getStringExtra("ComingFrom");
		   atharyEditor = athary.edit();
		   atharyEditor.putString("SAVE", extraData);
		   atharyEditor.commit();	
	}
   
 @Override
 protected void onResume() {
	 super.onResume();
 }
 
 @Override
 protected void onDestroy() {
 	super.onDestroy();
 }
    	
}
class SettingsSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
    String[] values;
    Context context;

    public SettingsSpinnerAdapter(Context context, String[] values) {
        this.context = context;
        this.values = values;
    }

    @Override
    public int getCount() {
        return values.length;
    }

    @Override
    public Object getItem(int position) {
        return values[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        if (v == null) {
           LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           v = vi.inflate(R.layout.txt_spinner2, null);
        } 
		TextView tv=(TextView) v.findViewById(R.id.ind_child);
		//tv.setTypeface(Typeface.createFromAsset(getAssets(), "font2.ttf"),Typeface.NORMAL);
		tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
		tv.setText(values[position]);
		
		return v;  

    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        if (v == null) {
           LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           v = vi.inflate(R.layout.txt_spinner_dropdown, null);
        } 
		TextView tv=(TextView) v.findViewById(R.id.ind_child);
		//tv.setTypeface(Typeface.createFromAsset(getAssets(), "font2.ttf"),Typeface.NORMAL);
		tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
		tv.setBackgroundResource(R.drawable.custom_style_nocorners);
		tv.setTextColor(Color.argb(255, 134, 121, 81));

		tv.setText(values[position]);

		return v;  

    }
}