package com.hyperling.carbupbeta;

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

/**
 * Created by ling on 12/31/15.
 */
public class List extends ListInfo{

    LinearLayout linearLayout;
    Button btnDelete, btnEdit, btnItem;

    private DALIH dalih;

    private int textSize;

    public List(Context context, ListInfo listInfo){
        // Set attributes
        setID(listInfo.getID());

        dalih = new DALIH(context);

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

        // 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);

        // Add the values to the buttons
        btnDelete.setText(R.string.delete_button); // TODO: Make this an image
        btnEdit.setText(R.string.edit_button); // TODO: Make this a pencil image
        setName(listInfo.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);
                btnItem.setTextColor(getRandomColor());
                break;// Dark Mode
            case Configuration.UI_MODE_NIGHT_YES:
                // Night mode is active, we're using dark theme
                btnItem.setBackgroundColor(Color.DKGRAY);
                break;
        }
        /* */
        // Size!
        //textSize = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth()/30;
        btnDelete.setTextSize(16); //textSize-10);
        btnEdit.setTextSize(16); //textSize-10);
        btnItem.setTextSize(18); //textSize-6);

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

    public LinearLayout getView(){
        return linearLayout;
    }

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

    public int getRandomColor(){
        // Make negative to turn feature off
        int choice = (int) (Math.random()*12); // Highest case + 2 includes default

        //if (dalih.DEBUG) System.out.println(getName() + " is color " + choice);

        switch(choice){
            // Random assortment of colors
            case 0: return Color.rgb(250, 0, 0); // red
            case 1: return Color.rgb(250, 150, 0); // orange
            case 2: return Color.rgb(200, 200, 0); // yellow
            case 3: return Color.rgb(0, 250, 0); // green
            case 4: return Color.rgb(0, 150, 250); // aqua
            case 5: return Color.rgb(0, 0, 250); // blue
            case 6: return Color.rgb(150, 0, 250); // soft purple
            case 7: return Color.rgb(200, 0, 200); // purple

            // Unused colors
            //case 8: return Color.rgb(0, 250, 250); // cyan
            case 9: return Color.rgb(125, 250, 0); // lime
            case 10: return Color.rgb(0, 250, 125); // turqoise?
            case 11: return Color.rgb(250, 0, 125); // magenta

            // Shades of grey
            //case 12: return  Color.rgb(200, 200, 200); // light grey
            case 8: return  Color.rgb(100, 100, 100); // dark grey

            // Possible theme?
            case 69: return Color.rgb(250, 250, 0); // yellow
            case 70: return Color.rgb(125, 0, 250); // purple

            // Catch
            default: return Color.BLACK;
        }
    }

}
