package org.stepic.droid.store.dao;

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

import org.stepic.droid.model.Actions;
import org.stepic.droid.model.Section;
import org.stepic.droid.store.structure.DbStructureSections;
import org.stepic.droid.util.DbParseHelper;

public class SectionDaoImpl extends DaoBase<Section> {

    public SectionDaoImpl(SQLiteDatabase openHelper) {
        super(openHelper);
    }

    @Override
    public Section parsePersistentObject(Cursor cursor) {
        Section section = new Section();
//        section = new Section();

        int columnIndexId = cursor.getColumnIndex(DbStructureSections.Column.SECTION_ID);
        int columnIndexTitle = cursor.getColumnIndex(DbStructureSections.Column.TITLE);
        int columnIndexSlug = cursor.getColumnIndex(DbStructureSections.Column.SLUG);
        int columnIndexIsActive = cursor.getColumnIndex(DbStructureSections.Column.IS_ACTIVE);
        int columnIndexBeginDate = cursor.getColumnIndex(DbStructureSections.Column.BEGIN_DATE);
        int columnIndexSoftDeadline = cursor.getColumnIndex(DbStructureSections.Column.SOFT_DEADLINE);
        int columnIndexHardDeadline = cursor.getColumnIndex(DbStructureSections.Column.HARD_DEADLINE);
        int columnIndexCourseId = cursor.getColumnIndex(DbStructureSections.Column.COURSE);
        int columnIndexPosition = cursor.getColumnIndex(DbStructureSections.Column.POSITION);
        int columnIndexUnits = cursor.getColumnIndex(DbStructureSections.Column.UNITS);
        int indexIsCached = cursor.getColumnIndex(DbStructureSections.Column.IS_CACHED);
        int indexIsLoading = cursor.getColumnIndex(DbStructureSections.Column.IS_LOADING);
        int indexTestSection = cursor.getColumnIndex(DbStructureSections.Column.TEST_SECTION);

        section.setId(cursor.getLong(columnIndexId));
        section.setTitle(cursor.getString(columnIndexTitle));
        section.setSlug(cursor.getString(columnIndexSlug));
        section.set_active(cursor.getInt(columnIndexIsActive) > 0);
        section.setBegin_date(cursor.getString(columnIndexBeginDate));
        section.setSoft_deadline(cursor.getString(columnIndexSoftDeadline));
        section.setHard_deadline(cursor.getString(columnIndexHardDeadline));
        section.setCourse(cursor.getLong(columnIndexCourseId));
        section.setPosition(cursor.getInt(columnIndexPosition));
        section.set_cached(cursor.getInt(indexIsCached) > 0);
        section.set_loading(cursor.getInt(indexIsLoading) > 0);
        section.setUnits(DbParseHelper.INSTANCE.parseStringToLongArray(cursor.getString(columnIndexUnits)));

        String test_section = cursor.getString(indexTestSection);
        Actions actions = new Actions();
        actions.setTest_section(test_section);
        section.setActions(actions);

        return section;
    }

    @Override
    public String getDbName() {
        return DbStructureSections.SECTIONS;
    }

    @Override
    public ContentValues getContentValues(Section section) {
        ContentValues values = new ContentValues();

        values.put(DbStructureSections.Column.SECTION_ID, section.getId());
        values.put(DbStructureSections.Column.TITLE, section.getTitle());
        values.put(DbStructureSections.Column.SLUG, section.getSlug());
        values.put(DbStructureSections.Column.IS_ACTIVE, section.is_active());
        values.put(DbStructureSections.Column.BEGIN_DATE, section.getBegin_date());
        values.put(DbStructureSections.Column.SOFT_DEADLINE, section.getSoft_deadline());
        values.put(DbStructureSections.Column.HARD_DEADLINE, section.getHard_deadline());
        values.put(DbStructureSections.Column.COURSE, section.getCourse());
        values.put(DbStructureSections.Column.POSITION, section.getPosition());
        values.put(DbStructureSections.Column.UNITS, DbParseHelper.parseLongArrayToString(section.getUnits()));

        if (section.getActions() != null && section.getActions().getTest_section()!=null){
            values.put(DbStructureSections.Column.TEST_SECTION, section.getActions().getTest_section());
        }

        return values;
    }


    @Override
    public String getDefaultPrimaryColumn() {
        return DbStructureSections.Column.SECTION_ID;
    }

    @Override
    public String getDefaultPrimaryValue(Section persistentObject) {
        return persistentObject.getId() + "";
    }
}

@@ -179,32 +179,85 @@
 .model.Actions;%0A
+import org.stepic.droid.model.DiscountingPolicyType;%0A
 import org.stepi
@@ -1889,16 +1889,123 @@
 ECTION);
+%0A        int indexDiscountingPolicy = cursor.getColumnIndex(DbStructureSections.Column.DISCOUNTING_POLICY);
 %0A%0A      
@@ -2826,24 +2826,172 @@
 dexUnits)));
+%0A        int typeId = cursor.getInt(indexDiscountingPolicy);%0A        DiscountingPolicyType discountingPolicyType = getDiscountingPolicyType(typeId);
 %0A%0A        St
@@ -3195,32 +3195,340 @@
 section;%0A    %7D%0A%0A
+    private DiscountingPolicyType getDiscountingPolicyType(int typeId) %7B%0A        DiscountingPolicyType%5B%5D localValues = DiscountingPolicyType.values();%0A        if (typeId %3E= 0 && typeId %3C localValues.length) %7B%0A            return localValues%5BtypeId%5D;%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -4569,16 +4569,285 @@
 its()));
+%0A        if (section.getDiscountingPolicy() != null) %7B%0A            values.put(DbStructureSections.Column.DISCOUNTING_POLICY, section.getDiscountingPolicy().ordinal());%0A        %7D else %7B%0A            values.put(DbStructureSections.Column.DISCOUNTING_POLICY, -1);%0A        %7D
 %0A%0A      
@@ -4926,15 +4926,18 @@
 on()
+ 
 !=
+ 
 null)
+ 
 %7B%0A  

add discounting policy to dao