package org.stepic.droid.store.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;

import org.stepic.droid.model.Block;
import org.stepic.droid.model.BlockPersistentWrapper;
import org.stepic.droid.store.structure.DbStructureBlock;

public class BlockDaoImpl extends DaoBase<BlockPersistentWrapper> {
    public BlockDaoImpl(SQLiteOpenHelper openHelper) {
        super(openHelper);
    }

    @Override
    public BlockPersistentWrapper parsePersistentObject(Cursor cursor) {
        BlockPersistentWrapper blockPersistentWrapper = new BlockPersistentWrapper();

        int indexName = cursor.getColumnIndex(DbStructureBlock.Column.NAME);
        int indexText = cursor.getColumnIndex(DbStructureBlock.Column.TEXT);
        int indexStep = cursor.getColumnIndex(DbStructureBlock.Column.STEP_ID);

        Block block = new Block();
        block.setName(cursor.getString(indexName));
        block.setText(cursor.getString(indexText));

        blockPersistentWrapper.setBlock(block);
        blockPersistentWrapper.setStepId(cursor.getLong(indexStep));

        return blockPersistentWrapper;
    }

    @Override
    public ContentValues getContentValues(BlockPersistentWrapper blockWrapper) {
        ContentValues values = new ContentValues();
        if (blockWrapper.getBlock() == null) return values;

        values.put(DbStructureBlock.Column.STEP_ID, blockWrapper.getStepId());
        values.put(DbStructureBlock.Column.NAME, blockWrapper.getBlock().getName());
        values.put(DbStructureBlock.Column.TEXT, blockWrapper.getBlock().getText());

        return values;
    }

    @Override
    public String getDbName() {
        return DbStructureBlock.BLOCKS;
    }

    @Override
    public String getDefaultPrimaryColumn() {
        return DbStructureBlock.Column.STEP_ID;
    }

    @Override
    public String getDefaultPrimaryValue(BlockPersistentWrapper persistentObject) {
        return persistentObject.getStepId()+"";
    }
}

@@ -150,16 +150,59 @@
 elper;%0A%0A
+import org.jetbrains.annotations.Nullable;%0A
 import o
@@ -312,40 +312,276 @@
 oid.
-store.structure.DbStructureBlock
+model.CachedVideo;%0Aimport org.stepic.droid.model.Video;%0Aimport org.stepic.droid.model.VideoUrl;%0Aimport org.stepic.droid.store.structure.DbStructureBlock;%0Aimport org.stepic.droid.store.structure.DbStructureCachedVideo;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List
 ;%0A%0Ap
@@ -647,16 +647,64 @@
 pper%3E %7B%0A
+    private final IDao%3CCachedVideo%3E mVideoDao;%0A%0A
     publ
@@ -746,16 +746,44 @@
 enHelper
+, IDao%3CCachedVideo%3E videoDao
 ) %7B%0A    
@@ -805,16 +805,46 @@
 elper);%0A
+        mVideoDao = videoDao;%0A
     %7D%0A%0A 
@@ -2402,11 +2402,1682 @@
 %22;%0A    %7D
+%0A%0A    @Nullable%0A    @Override%0A    public BlockPersistentWrapper get(String whereColumn, String whereValue) %7B%0A        BlockPersistentWrapper blockWrapper = super.get(whereColumn, whereValue);%0A        addVideoToBlockWrapper(blockWrapper);%0A        return blockWrapper;%0A    %7D%0A%0A    @Override%0A    protected List%3CBlockPersistentWrapper%3E getAllWithQuery(String query, String%5B%5D whereArgs) %7B%0A        List%3CBlockPersistentWrapper%3E blockWrapperList = super.getAllWithQuery(query, whereArgs);%0A        for (BlockPersistentWrapper blockWrapperItem : blockWrapperList) %7B%0A            addVideoToBlockWrapper(blockWrapperItem);%0A        %7D%0A        return blockWrapperList;%0A    %7D%0A%0A    private void addVideoToBlockWrapper(BlockPersistentWrapper blockWrapper)%7B%0A        if (blockWrapper != null && blockWrapper.getBlock() != null) %7B%0A            CachedVideo video = mVideoDao.get(DbStructureCachedVideo.Column.STEP_ID, blockWrapper.getStepId() + %22%22);%0A            blockWrapper.getBlock().setVideo(transformCachedVideoToRealVideo(video));%0A        %7D%0A    %7D%0A%0A    //// FIXME: 17.02.16 refactor this hack%0A    @Nullable%0A    private Video transformCachedVideoToRealVideo(CachedVideo video) %7B%0A        Video realVideo = null;%0A        if (video != null) %7B%0A            realVideo = new Video();%0A            realVideo.setId(video.getVideoId());%0A            realVideo.setThumbnail(video.getThumbnail());%0A            VideoUrl videoUrl = new VideoUrl();%0A            videoUrl.setQuality(video.getQuality());%0A            videoUrl.setUrl(video.getUrl());%0A%0A            List%3CVideoUrl%3E list = new ArrayList%3C%3E();%0A            list.add(videoUrl);%0A            realVideo.setUrls(list);%0A        %7D%0A        return realVideo;%0A    %7D
 %0A%7D%0A

Add inner video in blockDao