/*
 * Copyright 2015 Guillermo Orellana Ruiz
 *
 * 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 es.guillermoorellana.travisforandroid.ui.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import es.guillermoorellana.travisforandroid.R;
import es.guillermoorellana.travisforandroid.api.entity.Build;
import es.guillermoorellana.travisforandroid.api.entity.BuildHistory;

public class BuildHistoryAdapter extends ItemClickableAdapter<BuildHistoryAdapter.BuildViewHolder> {
    @NonNull protected BuildHistory mData = new BuildHistory();

    @Override
    protected BuildViewHolder createViewHolder(View view) {
        return new BuildViewHolder(view);
    }

    @Override
    public BuildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(BuildViewHolder holder, int position) {
        holder.bind(getItem(position));
    }

    protected int getItemLayout() {
        return R.layout.item_build;
    }

    public Build getItem(int adapterPosition) {
        return mData.getBuilds().get(adapterPosition);
    }

    public void setData(@NonNull BuildHistory data) {
        mData = data; // Prevent possible side-effects.
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData.getBuilds().size();
    }

    @SuppressLint("SetTextI18n")
    public static class BuildViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.buildStatus) TextView buildStatus;
        @Bind(R.id.starter) TextView starter;
        @Bind(R.id.branch) TextView branch;
        @Bind(R.id.duration) TextView duration;
        @Bind(R.id.finishedAgo) TextView finishedAgo;

        public BuildViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(Build build) {
            buildStatus.setText("Build #" + build.getNumber());
            starter.setText("Started by:");
            branch.setText("Branch:");
            duration.setText("Duration:");
            finishedAgo.setText("Finished:");
        }
    }
}

@@ -1117,16 +1117,80 @@
 History;
+%0Aimport es.guillermoorellana.travisforandroid.api.entity.Commit;
 %0A%0Apublic
@@ -1731,20 +1731,22 @@
     
-holder.bind(
+Build build = 
 getI
@@ -1758,16 +1758,290 @@
 osition)
+;%0A        Commit relatedCommit = null;%0A        for (Commit commit : mData.getCommits()) %7B%0A            if (build.getCommitId() == commit.getId()) %7B%0A                relatedCommit = commit;%0A                break;%0A            %7D%0A        %7D%0A        holder.bind(build, relatedCommit
 );%0A    %7D
@@ -2998,16 +2998,38 @@
 ld build
+, Commit relatedCommit
 ) %7B%0A    
@@ -3100,23 +3100,24 @@
         
-starter
+duration
 .setText
@@ -3122,150 +3122,321 @@
 xt(%22
-Started by:%22);%0A            branch.setText(%22Branch:%22);%0A            duration.setText(%22Duration:%22);%0A            finishedAgo.setText(%22Finished:%22);
+Duration:%22 + build.getDuration());%0A            finishedAgo.setText(%22Finished:%22 + build.getFinishedAt());%0A            if (relatedCommit != null) %7B%0A                starter.setText(%22Started by:%22 + relatedCommit.getCommitterName());%0A                branch.setText(%22Branch:%22 + relatedCommit.getBranch());%0A            %7D
 %0A   

add details to build view