package com.acme.structure.util;


import com.acme.structure.BaseEntity;
import com.acme.structure.SuperEntity;
import com.acme.structure.component.BaseRow;
import com.acme.structure.util.datalist.StaticData;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.faces.model.SelectItem;

import oracle.jbo.domain.Timestamp;


public class ConversionUtil
{
    public static InputStream convertFileToInputStream(File file) throws FileNotFoundException, IOException
    {
        ByteArrayInputStream bais = null;
        try
        {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line;
            String content = "";
            while ((line = bufferedReader.readLine()) != null)
            {
                content = content + line;
            }
            bais = new ByteArrayInputStream(content.getBytes());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return bais;
    }

    public static byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException
    {
        //byte[] file = org.apache.commons.io.IOUtils.toByteArray(inputStream);
        //if apache jar not exist below snippet works
        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while ((bytesRead = inputStream.read(buffer)) != -1)
        {
            output.write(buffer, 0, bytesRead);
        }
        return output.toByteArray();
    }
    
    public static InputStream convertByteArrayToInputStream(byte[] file)
    {
        return new ByteArrayInputStream(file);
    }

    public static Timestamp convertDateToJBOTimeStamp(Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return new oracle.jbo.domain.Timestamp(cal.getTime());
    }

    public static Date convertJBOTimeStampToDate(oracle.jbo.domain.Timestamp date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());
        return cal.getTime();
    }

    public static Collection<BaseRow> getTransformedBaseRowCollection(List<BaseEntity> collection)
    {
        Collection<BaseRow> tmpCollection = new ArrayList<BaseRow>();
        Iterator iterator = collection.iterator();
        BaseRow row = null;
        while (iterator.hasNext())
        {
            row = new BaseRow((BaseEntity)iterator.next());
            tmpCollection.add(row);
        }
        return tmpCollection;
    }

    public static Collection getConvertedToSelectItemList(Collection list, boolean blankItemApplied)
    {
        Collections.sort((List)list);
        Collection convertedCollection = new ArrayList();
        SelectItem selectItem = null;
        SuperEntity superEntity = null;
        Iterator itr = list.iterator();
        if (blankItemApplied)
        {
            selectItem = new SelectItem();
            convertedCollection.add(selectItem);
        }
        while (itr.hasNext())
        {
            superEntity = (SuperEntity)itr.next();
            selectItem = getConvertedToSelectItem(superEntity);
            convertedCollection.add(selectItem);
        }

        return convertedCollection;
    }

    public static SelectItem getConvertedToSelectItem(SuperEntity superEntity)
    {
        SelectItem selectItem = new SelectItem();

        selectItem.setLabel(superEntity.toString());
        selectItem.setValue(superEntity);

        return selectItem;
    }

    public static Collection getConvertedToSelectItemListWithPK(Collection list, boolean blankItemApplied)
    {
        Collections.sort((List)list);
        Collection convertedCollection = new ArrayList();
        SelectItem selectItem = null;
        SuperEntity superEntity = null;
        Iterator itr = list.iterator();
        if (blankItemApplied)
        {
            selectItem = new SelectItem();
            convertedCollection.add(selectItem);
        }
        while (itr.hasNext())
        {
            superEntity = (SuperEntity)itr.next();
            selectItem = new SelectItem();
            selectItem.setLabel(superEntity.toString());
            selectItem.setValue(superEntity.getPrimaryKeyValue());
            convertedCollection.add(selectItem);
        }

        return convertedCollection;
    }

    public static Collection getConvertedToRadioSelectItemList(Collection list, boolean blankItemApplied)
    {
        Collection convertedCollection = new ArrayList();
        SelectItem selectItem = null;
        SuperEntity superEntity = null;
        Iterator itr = list.iterator();
        if (blankItemApplied)
        {
            selectItem = new SelectItem();
            convertedCollection.add(selectItem);
        }
        while (itr.hasNext())
        {
            selectItem = new SelectItem();
            superEntity = (SuperEntity)itr.next();
            selectItem = getConvertedToSelectItem(superEntity);
            convertedCollection.add(selectItem);
        }

        return convertedCollection;
    }

    public static Collection getConvertedToRadioSelectItemListWithStaticData(Collection<StaticData> list, boolean blankItemApplied)
    {
        Collection convertedCollection = new ArrayList();
        SelectItem selectItem = null;
        StaticData staticData = null;
        Iterator itr = list.iterator();
        if (blankItemApplied)
        {
            selectItem = new SelectItem();
            convertedCollection.add(selectItem);
        }
        while (itr.hasNext())
        {
            staticData = (StaticData)itr.next();
            selectItem = new SelectItem();
            selectItem.setValue(staticData.getValue());
            selectItem.setLabel(staticData.getDisplayValue());
            convertedCollection.add(selectItem);
        }

        return convertedCollection;
    }

    public static Collection getConvertedToSelectItemStringValueListWithStaticData(Collection<StaticData> list, boolean blankItemApplied)
    {
        Collection convertedCollection = new ArrayList();
        SelectItem selectItem = null;
        StaticData staticData = null;
        Iterator itr = list.iterator();
        if (blankItemApplied)
        {
            selectItem = new SelectItem();
            convertedCollection.add(selectItem);
        }
        while (itr.hasNext())
        {
            staticData = (StaticData)itr.next();
            selectItem = new SelectItem();
            selectItem.setValue(staticData.getDisplayValue());
            selectItem.setLabel(staticData.getDisplayValue());
            convertedCollection.add(selectItem);
        }

        return convertedCollection;
    }
    
    public static Collection getConvertedToSelectItemListWithStaticData(Collection<StaticData> list, boolean blankItemApplied)
    {
        Collection convertedCollection = new ArrayList();
        SelectItem selectItem = null;
        StaticData staticData = null;
        Iterator itr = list.iterator();
        if (blankItemApplied)
        {
            selectItem = new SelectItem();
            convertedCollection.add(selectItem);
        }
        while (itr.hasNext())
        {
            staticData = (StaticData)itr.next();
            selectItem = new SelectItem();
            selectItem.setValue(staticData.getValue());
            selectItem.setLabel(staticData.getDisplayValue());
            convertedCollection.add(selectItem);
        }

        return convertedCollection;
    }
}

