package mindpop.learnpop;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import org.w3c.dom.Text;


/**
 * A simple {@link Fragment} subclass.
 */
public class WebItem extends Fragment {


    private Resource resource;
    private TextView title;
    private TextView summary;
    private TextView likesTxt;
    private TextView unlikesTxt;
    private Button viewButton;
    private CheckBox fav_button;
    private CheckBox like_button;
    private CheckBox unlike_button;

    public WebItem() {
        // Required empty public constructor
    }

    public void init(Resource res){this.resource = res;}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_web_item, container, false);

        title = (TextView)rootView.findViewById(R.id.titleTxt);
        summary = (TextView)rootView.findViewById(R.id.sumTXT);
        viewButton = (Button) rootView.findViewById(R.id.btnView);
        likesTxt = (TextView) rootView.findViewById(R.id.numLikes);
        unlikesTxt = (TextView) rootView.findViewById(R.id.numDislikes);
        fav_button = (CheckBox) rootView.findViewById(R.id.fav_check);
        like_button = (CheckBox) rootView.findViewById(R.id.check_like);
        unlike_button = (CheckBox) rootView.findViewById(R.id.check_dislike);



        title.setText(resource.getTitle());
        summary.setText(resource.getSummary());
        updateLikeValues();

        viewButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                WebViewFragment webFrag = new WebViewFragment();
                webFrag.init(resource.getUrl());
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, webFrag).addToBackStack(null).commit();
            }
        });

        like_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //check if unlike button is pressed and eliminate press
                if(unlike_button.isChecked()){
                    unlike_button.setChecked(false);
                    resource.setDownVote(resource.getDownVote() -1);
                }else if(like_button.isChecked()){
                    //check if it was already checked to take away vote
                    resource.setUpVote(resource.getUpVote() -1);
                }else{
                    //addd 1 to resource
                    resource.setUpVote(resource.getUpVote() + 1);
                }

                updateLikeValues();

            }
        });

        unlike_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //check if unlike button is pressed and eliminate press
                if(like_button.isChecked()){
                    like_button.setChecked(false);
                    resource.setUpVote(resource.getUpVote() -1);
                }else if(unlike_button.isChecked()){
                    //check if it was already checked to take away vote
                    resource.setDownVote(resource.getDownVote() -1);
                }else{
                    //addd 1 to resource
                    resource.setDownVote(resource.getDownVote() + 1);
                }
                updateLikeValues();

            }
        });

        return rootView;
    }

    private void updateLikeValues(){
        like_button.setText(String.valueOf(resource.getUpVote()));
        unlikesTxt.setText(String.valueOf(resource.getDownVote()));
    }

}

@@ -2566,110 +2566,64 @@
 else
- if(like_button.isChecked())%7B%0A                    //check if it was already checked to take away vote%0A
+%7B%0A                    if(like_button.isChecked()) %7B%0A    
     
@@ -2670,37 +2670,42 @@
 rce.getUpVote() 
--
++ 
 1);%0A
+    
                 
@@ -2719,45 +2719,8 @@
     
-                //addd 1 to resource%0A
     
@@ -2767,38 +2767,60 @@
 rce.getUpVote() 
-+
+-
  1);%0A
+                    %7D%0A
                 
@@ -3274,16 +3274,37 @@
    %7Delse
+%7B%0A                   
  if(unli
@@ -3351,60 +3351,37 @@
     
-//check if it was already checked to take away vote%0A
+    //addd 1 to resource%0A    
     
@@ -3432,33 +3432,34 @@
 e.getDownVote() 
--
++ 
 1);%0A            
@@ -3454,32 +3454,36 @@
                 
+    
 %7Delse%7B%0A         
@@ -3485,41 +3485,8 @@
     
-            //addd 1 to resource%0A
     
@@ -3489,36 +3489,32 @@
                 
-    
 resource.setDown
@@ -3533,38 +3533,42 @@
 e.getDownVote() 
-+
+-
  1);%0A
+    
                 
@@ -3561,32 +3561,50 @@
               %7D%0A
+                %7D%0A
                 
@@ -3643,32 +3643,189 @@
  %7D%0A        %7D);%0A%0A
+        fav_button.setOnClickListener(new View.OnClickListener()%7B%0A            @Override%0A            public void onClick(View v)%7B%0A%0A            %7D%0A        %7D);%0A%0A
         return r

fix bug of updating values