package com.lemenzo.contacts;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowInsets;
import android.widget.*;

public final class Ui {
    public static final int BG=Color.rgb(8,9,12);
    public static final int SURFACE=Color.rgb(10,16,25);
    public static final int SURFACE2=Color.rgb(16,23,34);
    public static final int BLUE=Color.rgb(63,140,255);
    public static final int OUTLINE=Color.rgb(23,48,82);
    public static final int TEXT=Color.rgb(247,249,252);
    public static final int SECONDARY=Color.rgb(174,184,200);
    public static final int DANGER=Color.rgb(255,77,79);
    private Ui(){}

    public static int dp(Context c,int v){return Math.round(v*c.getResources().getDisplayMetrics().density);}

    public static GradientDrawable bg(Context c,int color,int radius){
        GradientDrawable d=new GradientDrawable();d.setColor(color);d.setCornerRadius(dp(c,radius));return d;
    }

    public static GradientDrawable outline(Context c,int color,int stroke,int radius){
        GradientDrawable d=bg(c,color,radius);d.setStroke(dp(c,1),stroke);return d;
    }

    public static TextView text(Context c,String s,int sp,int color){
        TextView v=new TextView(c);v.setText(s);v.setTextSize(sp);v.setTextColor(color);v.setGravity(Gravity.CENTER_VERTICAL);
        v.setFontFeatureSettings("kern");return v;
    }

    public static TextView title(Context c,String s,int sp){
        TextView v=text(c,s,sp,TEXT);v.setTypeface(Typeface.create("sans",Typeface.BOLD));return v;
    }

    public static TextView section(Context c,String s){
        TextView t=title(c,s.toUpperCase(),11);t.setTextColor(BLUE);t.setLetterSpacing(0.06f);return t;
    }

    public static ImageButton iconButton(Context c,int icon,String desc){
        ImageButton b=new ImageButton(c);b.setImageResource(icon);b.setContentDescription(desc);
        b.setBackground(outline(c,SURFACE,OUTLINE,16));b.setPadding(dp(c,12),dp(c,12),dp(c,12),dp(c,12));return b;
    }

    public static Button button(Context c,String s,boolean primary){
        Button b=new Button(c);b.setText(s);b.setAllCaps(false);b.setTextSize(15);b.setTypeface(Typeface.create("sans",Typeface.BOLD));
        b.setTextColor(TEXT);b.setBackground(outline(c,primary?BLUE:SURFACE,primary?BLUE:OUTLINE,15));b.setMinHeight(dp(c,50));return b;
    }

    public static EditText input(Context c,String hint){
        EditText e=new EditText(c);e.setHint(hint);e.setHintTextColor(SECONDARY);e.setTextColor(TEXT);e.setTextSize(15);
        e.setSingleLine();e.setPadding(dp(c,16),0,dp(c,16),0);e.setBackground(outline(c,SURFACE,OUTLINE,15));e.setMinHeight(dp(c,52));
        e.setSelectAllOnFocus(false);return e;
    }

    public static LinearLayout card(Context c){
        LinearLayout l=new LinearLayout(c);l.setOrientation(LinearLayout.VERTICAL);l.setPadding(dp(c,16),dp(c,14),dp(c,16),dp(c,14));
        l.setBackground(outline(c,SURFACE,OUTLINE,16));return l;
    }

    public static LinearLayout raisedCard(Context c){
        LinearLayout l=card(c);l.setBackground(outline(c,SURFACE2,OUTLINE,16));return l;
    }

    public static Space space(Context c,int h){Space s=new Space(c);s.setLayoutParams(new LinearLayout.LayoutParams(1,dp(c,h)));return s;}

    public static void applySafeInsets(Activity a,View root){
        root.setOnApplyWindowInsetsListener((v,in)->{
            android.graphics.Insets top=in.getInsets(WindowInsets.Type.statusBars()|WindowInsets.Type.displayCutout());
            android.graphics.Insets bottom=in.getInsets(WindowInsets.Type.navigationBars());
            v.setPadding(v.getPaddingLeft(),top.top+dp(a,8),v.getPaddingRight(),bottom.bottom+dp(a,8));
            return in;
        });root.requestApplyInsets();
    }

    public static LinearLayout subHeader(Activity a,String title){
        LinearLayout bar=new LinearLayout(a);bar.setGravity(Gravity.CENTER_VERTICAL);bar.setPadding(dp(a,16),0,dp(a,16),0);
        ImageButton back=new ImageButton(a);back.setImageResource(R.drawable.ic_back);back.setContentDescription(a.getString(R.string.back));
        back.setBackgroundColor(Color.TRANSPARENT);back.setPadding(dp(a,10),dp(a,10),dp(a,10),dp(a,10));
        bar.addView(back,new LinearLayout.LayoutParams(dp(a,44),dp(a,52)));
        TextView t=title(a,title,18);t.setGravity(Gravity.CENTER);bar.addView(t,new LinearLayout.LayoutParams(0,dp(a,56),1));
        Space end=new Space(a);bar.addView(end,new LinearLayout.LayoutParams(dp(a,44),dp(a,52)));
        back.setOnClickListener(v->a.onBackPressed());return bar;
    }

    public static LinearLayout bottomNav(Activity a,int selected,View.OnClickListener contacts,View.OnClickListener fav,View.OnClickListener groups,View.OnClickListener more){
        LinearLayout nav=new LinearLayout(a);nav.setGravity(Gravity.CENTER);nav.setPadding(dp(a,8),dp(a,6),dp(a,8),dp(a,4));
        nav.setBackground(outline(a,SURFACE,OUTLINE,18));
        addNavItem(a,nav,R.drawable.ic_contacts,R.string.contacts,0,selected,contacts);
        addNavItem(a,nav,R.drawable.ic_star,R.string.favorites,1,selected,fav);
        addNavItem(a,nav,R.drawable.ic_groups,R.string.groups,2,selected,groups);
        if(more!=null)addNavItem(a,nav,R.drawable.ic_more,R.string.more,3,selected,more);
        return nav;
    }

    private static void addNavItem(Activity a,LinearLayout nav,int icon,int label,int index,int selected,View.OnClickListener click){
        LinearLayout item=new LinearLayout(a);item.setOrientation(LinearLayout.VERTICAL);item.setGravity(Gravity.CENTER);
        ImageView iv=new ImageView(a);iv.setImageResource(icon);iv.setColorFilter(index==selected?BLUE:SECONDARY);
        TextView tx=text(a,a.getString(label),11,index==selected?BLUE:SECONDARY);tx.setGravity(Gravity.CENTER);
        item.addView(iv,new LinearLayout.LayoutParams(dp(a,24),dp(a,24)));item.addView(tx,new LinearLayout.LayoutParams(-1,dp(a,20)));
        item.setOnClickListener(click);nav.addView(item,new LinearLayout.LayoutParams(0,dp(a,54),1));
    }

    public static LinearLayout actionTile(Context c,int icon,String label){
        LinearLayout tile=new LinearLayout(c);tile.setOrientation(LinearLayout.VERTICAL);tile.setGravity(Gravity.CENTER);
        tile.setPadding(dp(c,8),dp(c,10),dp(c,8),dp(c,8));tile.setBackground(outline(c,SURFACE,OUTLINE,15));
        ImageView iv=new ImageView(c);iv.setImageResource(icon);tile.addView(iv,new LinearLayout.LayoutParams(dp(c,24),dp(c,24)));
        TextView t=text(c,label,11,TEXT);t.setGravity(Gravity.CENTER);tile.addView(t,new LinearLayout.LayoutParams(-1,dp(c,22)));
        return tile;
    }


    public static final class DialogParts {
        public final Dialog dialog;
        public final LinearLayout body;
        public final Button negative;
        public final Button positive;
        DialogParts(Dialog dialog,LinearLayout body,Button negative,Button positive){
            this.dialog=dialog;this.body=body;this.negative=negative;this.positive=positive;
        }
    }

    public static DialogParts showDialog(
            Activity a,
            String title,
            String message,
            View customContent,
            String negativeText,
            String positiveText,
            boolean danger) {
        Dialog d=new Dialog(a);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LinearLayout shell=new LinearLayout(a);
        shell.setOrientation(LinearLayout.VERTICAL);
        shell.setPadding(dp(a,20),dp(a,20),dp(a,20),dp(a,18));
        shell.setBackground(outline(a,SURFACE,OUTLINE,22));

        TextView titleView=title(a,title,20);
        shell.addView(titleView,new LinearLayout.LayoutParams(-1,-2));

        if(message!=null&&!message.isEmpty()){
            shell.addView(space(a,8));
            TextView msg=text(a,message,14,SECONDARY);
            msg.setLineSpacing(0f,1.08f);
            shell.addView(msg,new LinearLayout.LayoutParams(-1,-2));
        }

        LinearLayout body=new LinearLayout(a);
        body.setOrientation(LinearLayout.VERTICAL);
        if(customContent!=null){
            shell.addView(space(a,16));
            body.addView(customContent,new LinearLayout.LayoutParams(-1,-2));
            shell.addView(body,new LinearLayout.LayoutParams(-1,-2));
        }

        shell.addView(space(a,20));

        LinearLayout actions=new LinearLayout(a);
        actions.setOrientation(LinearLayout.HORIZONTAL);
        actions.setGravity(Gravity.END);

        Button negative=button(a,negativeText,false);
        Button positive=button(a,positiveText,true);
        if(danger){
            positive.setTextColor(TEXT);
            positive.setBackground(outline(a,DANGER,DANGER,15));
        }

        LinearLayout.LayoutParams half=new LinearLayout.LayoutParams(0,dp(a,50),1f);
        actions.addView(negative,half);
        actions.addView(space(a,10),new LinearLayout.LayoutParams(dp(a,10),1));
        actions.addView(positive,new LinearLayout.LayoutParams(0,dp(a,50),1f));
        shell.addView(actions,new LinearLayout.LayoutParams(-1,-2));

        d.setContentView(shell);
        d.setCanceledOnTouchOutside(true);
        Window w=d.getWindow();
        if(w!=null)w.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        d.show();
        w=d.getWindow();
        if(w!=null){
            int screen=a.getResources().getDisplayMetrics().widthPixels;
            w.setLayout(Math.min(screen-dp(a,32),dp(a,460)),WindowManager.LayoutParams.WRAP_CONTENT);
            w.setDimAmount(0.72f);
            w.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        negative.setOnClickListener(v->d.dismiss());
        return new DialogParts(d,body,negative,positive);
    }

    public static Dialog showChoiceDialog(
            Activity a,
            String title,
            String message,
            String first,
            String second,
            View.OnClickListener firstClick,
            View.OnClickListener secondClick,
            View.OnClickListener cancelClick) {
        Dialog d=new Dialog(a);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LinearLayout shell=new LinearLayout(a);
        shell.setOrientation(LinearLayout.VERTICAL);
        shell.setPadding(dp(a,20),dp(a,20),dp(a,20),dp(a,18));
        shell.setBackground(outline(a,SURFACE,OUTLINE,22));

        shell.addView(title(a,title,20));
        if(message!=null&&!message.isEmpty()){
            shell.addView(space(a,8));
            TextView msg=text(a,message,14,SECONDARY);
            shell.addView(msg);
        }

        shell.addView(space(a,16));
        Button one=button(a,first,false);
        one.setTextColor(TEXT);
        shell.addView(one,new LinearLayout.LayoutParams(-1,dp(a,52)));
        shell.addView(space(a,10));
        Button two=button(a,second,false);
        two.setTextColor(TEXT);
        shell.addView(two,new LinearLayout.LayoutParams(-1,dp(a,52)));
        shell.addView(space(a,16));
        Button cancel=button(a,a.getString(R.string.cancel),false);
        cancel.setTextColor(SECONDARY);
        shell.addView(cancel,new LinearLayout.LayoutParams(-1,dp(a,50)));

        d.setContentView(shell);
        d.setCanceledOnTouchOutside(true);
        d.setOnCancelListener(x->{if(cancelClick!=null)cancelClick.onClick(null);});
        one.setOnClickListener(v->{d.dismiss();if(firstClick!=null)firstClick.onClick(v);});
        two.setOnClickListener(v->{d.dismiss();if(secondClick!=null)secondClick.onClick(v);});
        cancel.setOnClickListener(v->{d.dismiss();if(cancelClick!=null)cancelClick.onClick(v);});

        Window w=d.getWindow();
        if(w!=null)w.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        d.show();
        w=d.getWindow();
        if(w!=null){
            int screen=a.getResources().getDisplayMetrics().widthPixels;
            w.setLayout(Math.min(screen-dp(a,32),dp(a,460)),WindowManager.LayoutParams.WRAP_CONTENT);
            w.setDimAmount(0.72f);
            w.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        return d;
    }

    public static void styleDialog(AlertDialog d){
        if(d==null)return;
        if(d.getWindow()!=null)d.getWindow().setBackgroundDrawable(outline(d.getContext(),SURFACE,OUTLINE,18));
        d.setOnShowListener(x->{
            Button p=d.getButton(AlertDialog.BUTTON_POSITIVE);if(p!=null)p.setTextColor(BLUE);
            Button n=d.getButton(AlertDialog.BUTTON_NEGATIVE);if(n!=null)n.setTextColor(SECONDARY);
        });
    }

    public static LinearLayout settingRow(Context c,String title,String value){
        LinearLayout row=raisedCard(c);row.setOrientation(LinearLayout.HORIZONTAL);row.setGravity(Gravity.CENTER_VERTICAL);
        TextView left=text(c,title,14,TEXT);row.addView(left,new LinearLayout.LayoutParams(0,dp(c,46),1));
        TextView right=text(c,value,13,SECONDARY);right.setGravity(Gravity.END|Gravity.CENTER_VERTICAL);
        row.addView(right,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,dp(c,46)));return row;
    }
}
