package com.jarulla.katnax;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqLiteOpenHelper extends SQLiteOpenHelper {
	
	private static final String strSandanNami="DbXjQli.db";
	private static final int intSandanNushisi=1;

	public MySqLiteOpenHelper(Context context) {
		super(context, strSandanNami, null, intSandanNushisi);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("create table urumchiQatnashLiniyesi (id integer primary key autoincrement, yolNami nvarchar(50),biketler ntext)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
	}//onUpgrade
	
    public void SandanniOzgertish(String strSql)
    {
    	//MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(context);
    	SQLiteDatabase db = this.getWritableDatabase();
    	db.execSQL(strSql);
    	db.close();    
    }//SandanniOzgertish
    
    public void SandanniOzgertish(String strSql,String []strArgs)
    {
    	//MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(context);
    	SQLiteDatabase db = this.getWritableDatabase();
    	db.execSQL(strSql, strArgs);
    	db.close();
    }//SandanniOzgertish
    
    public String BirHatireIzdesh(String strSql,String []strArgs,String strSitonNami)
	{
    	//MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(context);
    	SQLiteDatabase db=this.getReadableDatabase();
    	Cursor cur=db.rawQuery(strSql, strArgs);
    	if(cur.moveToFirst())
    	{
    		String strBiket=cur.getString(cur.getColumnIndex(strSitonNami));
    		return strBiket;
    	}
    	else
    	{
    		return "";
    	}
	}//BirHatireIzdesh
    
    public String KopHatireIzdesh(String strSql,String []strArgs,String strSitonNami)
    {
    	String strRet="";
    	//MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(context);
    	SQLiteDatabase db=this.getReadableDatabase();
    	Cursor cur=db.rawQuery(strSql, strArgs);
    	while(cur.moveToNext())
    	{
    		String strYolNami=cur.getString(cur.getColumnIndex(strSitonNami));
    		strRet=strRet+strYolNami+"�? ";
    	}//while
    	strRet=strRet.length()>0?strRet.substring(0,strRet.length()-2):"";
    	return strRet;
    }//KopHatireIzdesh
    
    public void HatireQoshush()
    {
    	//MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(context);
    	SQLiteDatabase db = this.getWritableDatabase();
//    	db.execSQL("insert into urumchiQatnashLiniyesi (yolNami,biketler) values ('1','1�?西八家户→烈士陵�?的途经公交站点：西八家户站 �?便民�?世禧�?�?�?红旗大队�?�?苏州路站 �?地磅�?�?天山砖厂�?�?鲤鱼山花园小区站 �?毛纺�?鲤鱼山南�?�?�?汇展园站 �?医学院站 �?八楼�?�?友好路站 �?明园�?�?西北路站 �?红山�?�?西大桥站 �?北门�?�?人民广场�?�?南门�?�?山西巷站 �?二道桥站 �?延安�?胜利�?�?�?友谊医院�?�?新疆大学�?�?三屯碑站 �?水库�?�?乌河管理处站 �?三环公司�?�?县水泥厂�?�?烈士陵园�?(�?0�?')");
//    	db.execSQL("insert into urumchiQatnashLiniyesi (yolNami,biketler) values ('2','2�?火车南站→机械厂)的途经公交站点：火车南�?�?长江路站 �?冷库�?�?碾子沟站 �?文化宫站 �?红山�?�?西虹路站 �?明园�?�?友好路站 �?八楼�?�?儿童公园�?�?科技馆站 �?大西沟站 �?大寨沟站 �?经管学院�?�?小西沟站 �?科学院站 �?铁路局�?�?新市区站 �?二宫�?�?财院�?�?农机厂站 �?轴承厂站 �?机械厂站 (�?4�?')");
//    	db.execSQL("insert into urumchiQatnashLiniyesi (yolNami,biketler) values ('3','3�?六道湾→仓房�?的途经公交站点：六道湾�?�?兵运司站 �?五星路站 �?青年路站 �?前进街站 �?东风路站 �?建国路站 �?幸福�?东环�?�?�?体育馆站 �?南门�?�?红旗路站 �?立交桥站 �?中国联通站 �?中医院站 �?七一酱园�?�?和平桥站 �?面粉厂站 �?十二中站 �?水泥厂站 �?36171医院�?�?供销公司�?�?仓房沟站 (�?2�?')");
//    	db.execSQL("insert into urumchiQatnashLiniyesi (yolNami,biketler) values ('4','4�?北门→七�?的途经公交站点：北门站 �?青年路站 �?汽配厂站 �?油库�?�?汽改厂站 �?虹桥�?�?牌坊�?�?搪瓷厂站 �?七纺�?(�?�?')");
//    	db.execSQL("insert into urumchiQatnashLiniyesi (yolNami,biketler) values ('5','5�?华凌市场→七道湾)的途经公交站点：华凌市场站 �?统管办站 �?市建委站 �?劳动街站 �?南湖小区�?�?南湖�?�?六道湾小学站 �?长青四队�?�?灭火处站 �?检查站 �?温泉路口�?�?四团�?�?苇湖庄站 �?测绘大队�?�?苇湖梁电厂站 �?苇湖梁煤矿站 �?地磅(苇湖�?�?�?市化工厂�?�?岔路口站 �?七道湾站 (�?0�?')");
//    	db.execSQL("insert into urumchiQatnashLiniyesi (yolNami,biketler) values ('5','5�?华凌市场→七道湾)的途经公交站点：华凌市场站 �?统管办站 �?市建委站 �?劳动街站 �?南湖小区�?�?南湖�?�?六道湾小学站 �?长青四队�?�?灭火处站 �?检查站 �?温泉路口�?�?四团�?�?苇湖庄站 �?测绘大队�?�?苇湖梁电厂站 �?苇湖梁煤矿站 �?地磅(苇湖�?�?�?市化工厂�?�?岔路口站 �?七道湾站 (�?0�?')");

    }

    private void ListAddRange(List<String> list,String []strArray)
    {
    	for(int i=0;i<strArray.length;i++)
    	{
    		list.add(strArray[i]);
    	}
	}
    
	public List<String> WastilikBaridighanLiniyeIzdesh(String strHazirqiBiket, String strNishanBiket)
    {
        List<String> listAlmishidighanBiketWeUzunluqi = new ArrayList<String>();
    	try
    	{
        //bezi biketlerge biwaste baridighan aptibus bolmisa, aptibus almiship barsimu bolidu. Almishidighan aptibusni korsitip beridu.
    	SQLiteDatabase db=this.getReadableDatabase();
        List<String> listHazirqiBikettinOtidighanAptibuslar=new ArrayList<String>();
        List<String> listHazirqiBikettinOtidighanAptibusBiketliri=new ArrayList<String>();
        Cursor cur=db.rawQuery("select yolNami,biketler from urumchiQatnashLiniyesi where biketler like ?", new String[]{"%"+strHazirqiBiket+"%"});
        while(cur.moveToNext())
        {
        	listHazirqiBikettinOtidighanAptibuslar.add(cur.getString(cur.getColumnIndex("yolNami")));
        	listHazirqiBikettinOtidighanAptibusBiketliri.add(cur.getString(cur.getColumnIndex("biketler")));
        }
          cur =db.rawQuery("select yolNami,biketler from urumchiQatnashLiniyesi where biketler like ?",new String[]{"%"+strNishanBiket+"%"});
          List<String> listNishanBiketkeBaridighanAptibuslar=new ArrayList<String>();
          List<String> listNishanBiketkeBaridighanAptibusBiketliri=new ArrayList<String>();
           while(cur.moveToNext())
           {
        	   listNishanBiketkeBaridighanAptibuslar.add(cur.getString(cur.getColumnIndex("yolNami")));
        	   listNishanBiketkeBaridighanAptibusBiketliri.add(cur.getString(cur.getColumnIndex("biketler")));
           }
         int intK=-1,intG=-1;

        for (int i = 0; i < listHazirqiBikettinOtidighanAptibusBiketliri.size(); i++)
        {
        	List<String>listHbiket=new ArrayList<String>();
        	ListAddRange(listHbiket,listHazirqiBikettinOtidighanAptibusBiketliri.get(i).split("→"));
        	listHbiket.remove(0);
        	listHbiket.set(0, listHbiket.get(0).substring(listHbiket.get(0).lastIndexOf("：")+1));

            for (int j = 0; j < listNishanBiketkeBaridighanAptibusBiketliri.size(); j++)
            {
            	List<String>listNbiket=new ArrayList<String>();
            	ListAddRange(listNbiket,listNishanBiketkeBaridighanAptibusBiketliri.get(j).split("→"));
            	listNbiket.remove(0);
            	listNbiket.set(0, listNbiket.get(0).substring(listNbiket.get(0).lastIndexOf("：")+1));
                for (int k = 0; k < listHbiket.size(); k++)
                {
                	if(listHbiket.get(k).indexOf(strHazirqiBiket)!=-1)
                		intK=k;
                    for (int g = 0; g < listNbiket.size(); g++)
                    {
                    	if(listNbiket.get(g).indexOf(strNishanBiket)!=-1)
                    		intG=g;
                        if (listHbiket.get(k).trim().contentEquals(listNbiket.get(g).trim()))
                        {
                            String strLiniye = "乘坐" + listHazirqiBikettinOtidighanAptibuslar.get(i).trim() + "路" + String.valueOf(Math.abs(intK-k)-1) + "站" + listHbiket.get(k).trim() + "下车，再乘坐" + listNishanBiketkeBaridighanAptibuslar.get(j).trim() + "，经" + String.valueOf(Math.abs(intG-g)-1) +"站到达目的\r\n";
                            if (listAlmishidighanBiketWeUzunluqi.indexOf(strLiniye)==-1)
                            {
                                listAlmishidighanBiketWeUzunluqi.add(strLiniye);
                            }
                        }
                    }
                }
            }
        }
        return listAlmishidighanBiketWeUzunluqi;
    	}
    	catch(Exception ex)
    	{
    		listAlmishidighanBiketWeUzunluqi.add(ex.getMessage());
    		ex.printStackTrace();
    		return listAlmishidighanBiketWeUzunluqi;
    	}
    }

}
