/*
 * Copyright 2018 Maximilian Irro
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.rometools.modules.psc.modules;

import com.rometools.modules.psc.types.SimpleChapter;
import com.rometools.rome.feed.CopyFrom;
import com.rometools.rome.feed.impl.EqualsBean;
import com.rometools.rome.feed.impl.ToStringBean;
import com.rometools.rome.feed.module.ModuleImpl;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * A Module implementation for item level Simple Chapter information.
 */
public class PodloveSimpleChapterModuleImpl
        extends ModuleImpl
        implements PodloveSimpleChapterModule, Cloneable, Serializable {

    private List<SimpleChapter> chapters;

    public PodloveSimpleChapterModuleImpl() {
        super(PodloveSimpleChapterModule.class, PodloveSimpleChapterModule.URI);
    }

    @Override
    public List<SimpleChapter> getChapters() {
        return (chapters==null) ? (chapters = new LinkedList<SimpleChapter>()) : chapters;
    }

    @Override
    public void setChapters(final List<SimpleChapter> chapters) {
        this.chapters = chapters;
    }

    @Override
    public Class<? extends CopyFrom> getInterface() {
        return PodloveSimpleChapterModule.class;
    }

    @Override
    public void copyFrom(final CopyFrom obj) {
        final PodloveSimpleChapterModule mod = (PodloveSimpleChapterModule) obj;
        final List<SimpleChapter> chapters = new LinkedList<SimpleChapter>();
        for(SimpleChapter c1 : mod.getChapters()) {
            final SimpleChapter c2 = new SimpleChapter();
            c2.copyFrom(c1);
            chapters.add(c2);
        }
        setChapters(chapters);
    }

    @Override
    public String getUri() {
        return PodloveSimpleChapterModule.URI;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        final PodloveSimpleChapterModuleImpl mod = new PodloveSimpleChapterModuleImpl();
        final List<SimpleChapter> result = new LinkedList<SimpleChapter>();
        for (SimpleChapter c1 : this.chapters){
            SimpleChapter c2 = new SimpleChapter();
            c2.copyFrom(c1);
            result.add(c2);
        }
        result.subList(0, result.size()); // not sure why I need to do this
        mod.setChapters(result);
        return mod;
    }

    @Override
    public boolean equals(final Object obj) {
        final EqualsBean eBean = new EqualsBean(PodloveSimpleChapterModuleImpl.class, this);
        return eBean.beanEquals(obj);
    }

    @Override
    public int hashCode() {
        final EqualsBean equals = new EqualsBean(PodloveSimpleChapterModuleImpl.class, this);
        return equals.beanHashCode();
    }

    @Override
    public String toString() {
        final ToStringBean tsBean = new ToStringBean(PodloveSimpleChapterModuleImpl.class, this);
        return tsBean.toString();
    }
}

@@ -2907,37 +2907,38 @@
  obj) %7B%0A        
-final
+return
  EqualsBean eBea
@@ -2936,31 +2936,19 @@
 Bean
- eBean = new 
+.bean
 Equals
-Bean
 (Pod
@@ -2990,43 +2990,10 @@
 this
-);%0A        return eBean.beanEquals(
+, 
 obj)
@@ -3055,115 +3055,25 @@
     
-final EqualsBean equals = new EqualsBean(PodloveSimpleChapterModuleImpl.class, this);%0A        return equals
+return EqualsBean
 .bea
@@ -3082,16 +3082,20 @@
 ashCode(
+this
 );%0A    %7D
@@ -3149,21 +3149,22 @@
         
-final
+return
  ToStrin
@@ -3172,34 +3172,17 @@
 Bean
- tsBean = new T
+.t
 oString
-Bean
 (Pod
@@ -3227,42 +3227,8 @@
 s);%0A
-        return tsBean.toString();%0A
     

Fix equals/hashCode/toString for podlove