package com.hyperling.carbupbeta;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

/**
 * Created by ling on 1/1/16.
 */
public class MySettingItem extends MySettingInfo{

    private LinearLayout linearLayout;
    private RelativeLayout relativeLayout;
    TextView btnItem;
    CheckBox checkBox;

    private int textSize;

    public MySettingItem(Context context, MySettingInfo info){
        // Instantiate the Views
        linearLayout = new LinearLayout(context);
        relativeLayout = new RelativeLayout(context);
        btnItem = new TextView(context);
        checkBox = new CheckBox(context);

        // Give the views their required attributes (Wrap Content)
        LayoutParams lay_lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        LayoutParams rel_lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        LayoutParams item_lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        RelativeLayout.LayoutParams check_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

        lay_lp.setMargins(4,4,4,4);
        linearLayout.setLayoutParams(lay_lp);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        relativeLayout.setLayoutParams(rel_lp);
        if (Build.VERSION.SDK_INT >= 17) {
            relativeLayout.setHorizontalGravity(RelativeLayout.ALIGN_PARENT_END);
        }
        else {
            relativeLayout.setHorizontalGravity(RelativeLayout.ALIGN_PARENT_RIGHT);
        }

        btnItem.setLayoutParams(item_lp);
        checkBox.setLayoutParams(check_lp);

        // Add the values to the buttons
        setName(info.getName());
        setEnabled(info.getEnabled());

        /* ========== Style the buttons ========== */
        /* Color! * /
        linearLayout.setBackgroundColor(Color.LTGRAY);
        relativeLayout.setBackgroundColor(Color.DKGRAY);
        btnItem.setBackgroundColor(Color.LTGRAY);
        btnItem.setTextColor(Color.BLACK);
        /* */
        // Size!
        //textSize = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth()/30;
        btnItem.setTextSize(24); // textSize);
        btnItem.setPadding(4,4,4,4);

        // Add the items to the layout!
        relativeLayout.addView(checkBox);

        linearLayout.addView(btnItem);
        linearLayout.addView(relativeLayout);

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= 15) {
                    btnItem.callOnClick();
                }
            }
        });

        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= 15) {
                    btnItem.callOnClick();
                }
            }
        });
    }

    public LinearLayout getView(){
        return linearLayout;
    }

    @Override
    public void setName(String name){
        super.setName(name);
        btnItem.setText(getName());
    }

    @Override
    public void setEnabled(boolean enabled){
        super.setEnabled(enabled);
        checkBox.setChecked(getEnabled());
    }
}
