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());
        likesTxt.setText(String.valueOf(resource.getUpVote()));
        unlikesTxt.setText(String.valueOf(resource.getDownVote()));

        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);
                }
                //addd 1 to resource
                resource.setUpVote(resource.getUpVote() + 1);

            }
        });

        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);
                }
                //addd 1 to resource
                resource.setDownVote(resource.getDownVote() + 1);

            }
        });

        return rootView;
    }


}

@@ -1762,129 +1762,25 @@
     
-likesTxt.setText(String.valueOf(resource.getUpVote()));%0A        unlikesTxt.setText(String.valueOf(resource.getDownVote())
+updateLikeValues(
 );%0A%0A
@@ -2464,32 +2464,101 @@
 Checked(false);%0A
+                    resource.setDownVote(resource.getDownVote() -1);%0A
                 
@@ -2551,32 +2551,33 @@
               %7D%0A
+%0A
                 
@@ -2650,32 +2650,68 @@
 etUpVote() + 1);
+%0A                updateLikeValues();
 %0A%0A            %7D%0A
@@ -3017,32 +3017,97 @@
 Checked(false);%0A
+                    resource.setUpVote(resource.getUpVote() -1);%0A
                 
@@ -3210,16 +3210,52 @@
 () + 1);
+%0A                updateLikeValues();
 %0A%0A      
@@ -3307,11 +3307,189 @@
 %0A    %7D%0A%0A
+    private void updateLikeValues()%7B%0A        like_button.setText(String.valueOf(resource.getUpVote()));%0A        unlikesTxt.setText(String.valueOf(resource.getDownVote()));%0A    %7D%0A
 %0A%7D%0A

update like values