package com.hyperling.carbupbeta;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.widget.LinearLayout;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;

import java.text.DecimalFormat;

/**
 * Created by usb on 9/28/14.
 */
public class ListItem extends ListItemInfo {

    private double calPerDol, carbPerCal;

    LinearLayout linearLayout;
    Button btnDelete, btnEdit, btnItem;

    private int textSize;

    public ListItem(Context context, ListItemInfo listItemInfo){

        // Instantiate the Views
        linearLayout = new LinearLayout(context);
        btnDelete = new Button(context);
        btnEdit = new Button(context);
        btnItem = new Button(context);

        setID(listItemInfo.getID());
        setName(listItemInfo.getName());
        setCost(listItemInfo.getCost());
        setServings(listItemInfo.getServings());
        setCalories(listItemInfo.getCalories());
        setCarbs(listItemInfo.getCarbs());
        setFiber(listItemInfo.getFiber());
        calPerDol = getCalPerDol();
        carbPerCal = getCarbPerCal();

        // Give the views their required attributes (Wrap Content)
        LinearLayout.LayoutParams lay_lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams delete_lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams edit_lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams item_lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        lay_lp.setMargins(1, 1, 1, 1);
        linearLayout.setLayoutParams(lay_lp);
        linearLayout.setPadding(1, 1, 1, 1);

        delete_lp.setMargins(1, 1, 1, 1);
        btnDelete.setLayoutParams(delete_lp);

        edit_lp.setMargins(1, 1, 1, 1);
        btnEdit.setLayoutParams(edit_lp);

        item_lp.setMargins(1, 1, 1, 1);
        btnItem.setLayoutParams(item_lp);

        // Load values
        btnDelete.setText(R.string.delete_button); // TODO: Make this an image?
        btnEdit.setText(R.string.edit_button); // TODO: Make this a pencil?
        setName(listItemInfo.getName                                                                                                                                                                                                                                                                                                                                ());

        // ========== Style the buttons ==========
        /* Color! */
        int currentNightMode =
            context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK
        ;
        switch (currentNightMode) {
            // Light Mode
            case Configuration.UI_MODE_NIGHT_NO:
                linearLayout.setBackgroundColor(Color.BLACK);
                btnDelete.setBackgroundColor(Color.LTGRAY);
                btnDelete.setTextColor(Color.BLACK);
                btnEdit.setBackgroundColor(Color.LTGRAY);
                btnEdit.setTextColor(Color.BLACK);
                btnItem.setBackgroundColor(Color.WHITE);
                break;
            // Dark Mode
            case Configuration.UI_MODE_NIGHT_YES:
                btnItem.setBackgroundColor(Color.DKGRAY);
                break;
        }
        /* */
        int[] colors = getColors(carbPerCal);
        btnItem.setTextColor(Color.rgb(colors[0], colors[1], colors[2]));
        // Size!
        //textSize = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth()/30;
        btnDelete.setTextSize(16); //textSize-10);
        btnEdit.setTextSize(16); //textSize-10);
        btnItem.setTextSize(16); //textSize-8);

        // Add the buttons to the layout!
        linearLayout.addView(btnDelete);
        linearLayout.addView(btnEdit);
        linearLayout.addView(btnItem);
    }

    public LinearLayout getView(){
        return linearLayout;
    }

    public int[] getColors(double carbPerCal){
        int r, g, b;
        b = 0;

        // Find the ratio
        double dRed = 1-carbPerCal;
        double dGreen = carbPerCal;

        // Find which is larger
        double max;
        if (dGreen > dRed){
            max = dGreen;
        } else {
            max = dRed;
        }

        // Find the secret number (diff)
        double diff = 1 - max;

        // Brighten the colors with diff!
        dRed += diff;
        dGreen += diff;

        // Bring the colors into spectrum!
        dRed *= 255;
        dGreen *= 255;

        // Make the numbers integers, increase to color spectrum
        r = (int) dRed;
        g = (int) dGreen;

        // Put them in an array to pack and ship away
        int[] colors = {r, g, b};

        if(carbPerCal >= 1){
            colors = new int[] {0, 255, 0};
        }

        return colors;
    }

    public double getCalPerDol(){
        // Total calories divided by $$$
        return (getCalories()*getServings())/getCost();
    }

    public double getCarbPerCal(){
        // Calories from carbs divided by calories from all sources
        return ((getCarbs()-getFiber())*4)/getCalories();
    }

    @Override
    public void setName(String name){
        super.setName(name);
        DecimalFormat format = new DecimalFormat("#.##");
        String strCalPerDol = format.format(calPerDol);
        strCalPerDol = getName() + "\n" + strCalPerDol + "Cal/$  " + format.format(carbPerCal * 100) + "%Carbs";
        btnItem.setText(strCalPerDol);
    }
}
