package com.akexorcist.sleepingforless.view.post;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.akexorcist.sleepingforless.R;
import com.akexorcist.sleepingforless.common.SFLActivity;
import com.akexorcist.sleepingforless.constant.Key;
import com.akexorcist.sleepingforless.network.blogger.BloggerManager;
import com.akexorcist.sleepingforless.network.blogger.model.Failure;
import com.akexorcist.sleepingforless.network.blogger.model.Post;
import com.akexorcist.sleepingforless.network.blogger.model.PostList;
import com.akexorcist.sleepingforless.util.content.ContentUtility;
import com.squareup.otto.Subscribe;

import org.parceler.Parcels;

import java.util.List;

public class DebugPostActivity extends SFLActivity implements CompoundButton.OnCheckedChangeListener {
    private LinearLayout layoutPostContent;
    private CheckBox cbPlainText;
    private CheckBox cbHeader;
    private CheckBox cbImage;
    private CheckBox cbCode;
    private CheckBox cbOther;
    private PostList.Item postItem;
    private Post post;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_debug);

        layoutPostContent = (LinearLayout) findViewById(R.id.layout_post_content);
        cbPlainText = (CheckBox) findViewById(R.id.cb_plain_text);
        cbHeader = (CheckBox) findViewById(R.id.cb_header);
        cbImage = (CheckBox) findViewById(R.id.cb_image);
        cbCode = (CheckBox) findViewById(R.id.cb_code);
        cbOther = (CheckBox) findViewById(R.id.cb_other);

        cbPlainText.setOnCheckedChangeListener(this);
        cbHeader.setOnCheckedChangeListener(this);
        cbImage.setOnCheckedChangeListener(this);
        cbCode.setOnCheckedChangeListener(this);
        cbOther.setOnCheckedChangeListener(this);

        if (savedInstanceState == null) {
            setupFirstRun();
        }
    }

    private void setupFirstRun() {
        postItem = Parcels.unwrap(getIntent().getParcelableExtra(Key.POST_ITEM));
        BloggerManager.getInstance().getPostById(postItem.getId());
    }

    @Subscribe
    public void onPostSucess(Post post) {
        this.post = post;
        setPostContent(post);
    }

    @Subscribe
    public void onPostFailure(Failure failure) {
        Log.e("Check", "onPostFailure");
    }

    private void setPostContent(Post post) {
        layoutPostContent.removeAllViews();
        List<String> textList = ContentUtility.getInstance().wrapContent(post.getContent());
        for (String text : textList) {
            if (ContentUtility.getInstance().isCode(text)) {
                if (cbCode.isChecked()) {
                    addContent(text, Color.parseColor("#e62150"));
                }
            } else if (ContentUtility.getInstance().isImage(text)) {
                if (cbImage.isChecked()) {
                    addContent(text, Color.parseColor("#1d6292"));
                }
            } else if (ContentUtility.getInstance().isHeaderText(text)) {
                if (cbHeader.isChecked()) {
                    addContent(text, Color.parseColor("#4b820b"));
                }
            } else if (ContentUtility.getInstance().isPlainText(text)) {
                if (cbPlainText.isChecked()) {
                    addContent("      " + text, Color.parseColor("#5a6a6c"));
                }
            } else {
                if (cbOther.isChecked()) {
                    addContent(text, Color.parseColor("#b3b3b3"));
                }
            }
        }
    }

    private void addContent(String plainText, int textColor) {
        View view = LayoutInflater.from(this).inflate(R.layout.view_post_content_plain_text, layoutPostContent, false);
        TextView tvPostContentPlainText = (TextView) view.findViewById(R.id.tv_post_content_plain_text);
        tvPostContentPlainText.setText(plainText);
        tvPostContentPlainText.setTextColor(textColor);
        layoutPostContent.addView(view);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        setPostContent(post);
    }
}

@@ -224,46 +224,8 @@
 ox;%0A
-import android.widget.CompoundButton;%0A
 impo
@@ -877,122 +877,195 @@
 t;%0A%0A
-public class DebugPostActivity extends SFLActivity implements CompoundButton.OnCheckedChangeListener %7B%0A    private
+import butterknife.Bind;%0Aimport butterknife.ButterKnife;%0Aimport butterknife.OnCheckedChanged;%0A%0Apublic class DebugPostActivity extends SFLActivity %7B%0A    @Bind(R.id.layout_post_content)%0A   
  Lin
@@ -1093,27 +1093,50 @@
 ontent;%0A
+%0A
     
-private
+@Bind(R.id.cb_plain_text)%0A   
  CheckBo
@@ -1150,27 +1150,46 @@
 inText;%0A
+%0A
     
-private
+@Bind(R.id.cb_header)%0A   
  CheckBo
@@ -1200,27 +1200,45 @@
 Header;%0A
+%0A
     
-private
+@Bind(R.id.cb_image)%0A   
  CheckBo
@@ -1248,27 +1248,44 @@
 bImage;%0A
+%0A
     
-private
+@Bind(R.id.cb_code)%0A   
  CheckBo
@@ -1294,27 +1294,45 @@
 cbCode;%0A
+%0A
     
-private
+@Bind(R.id.cb_other)%0A   
  CheckBo
@@ -1342,28 +1342,21 @@
 bOther;%0A
+%0A
     
-private 
 PostList
@@ -1375,24 +1375,16 @@
 em;%0A    
-private 
 Post pos
@@ -1561,646 +1561,8 @@
 );%0A%0A
-        layoutPostContent = (LinearLayout) findViewById(R.id.layout_post_content);%0A        cbPlainText = (CheckBox) findViewById(R.id.cb_plain_text);%0A        cbHeader = (CheckBox) findViewById(R.id.cb_header);%0A        cbImage = (CheckBox) findViewById(R.id.cb_image);%0A        cbCode = (CheckBox) findViewById(R.id.cb_code);%0A        cbOther = (CheckBox) findViewById(R.id.cb_other);%0A%0A        cbPlainText.setOnCheckedChangeListener(this);%0A        cbHeader.setOnCheckedChangeListener(this);%0A        cbImage.setOnCheckedChangeListener(this);%0A        cbCode.setOnCheckedChangeListener(this);%0A        cbOther.setOnCheckedChangeListener(this);%0A%0A
     
@@ -1630,32 +1630,64 @@
 un();%0A        %7D%0A
+        ButterKnife.bind(this);%0A
     %7D%0A%0A    priva
@@ -3759,93 +3759,141 @@
   @O
-verride%0A    public void onCheckedChanged(CompoundButton buttonView, boolean isCheck
+nCheckedChanged(%7BR.id.cb_plain_text, R.id.cb_header, R.id.cb_image, R.id.cb_code, R.id.cb_other%7D)%0A    public void contentTypeChang
 ed
+(
 ) %7B%0A

Use ButterKnife