package com.lemenzo.clock.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

public final class Ui {
    public static final int BLACK=0xff08090c,SURFACE=0xff07111e,SURFACE2=0xff0b1929,SURFACE3=0xff102641,
            SURFACE_RAISED=0xff122b49,BLUE=0xff3f8cff,BLUE_BRIGHT=0xff67a6ff,BLUE_SOFT=0xff102a4b,BLUE_PRESSED=0xff2f78e8,
            WHITE=0xfff8fbff,SILVER=0xffb7c8e6,MUTED=0xff86abd8,DANGER=0xffff5a67;
    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 g=new GradientDrawable();g.setColor(color);g.setCornerRadius(dp(c,radius));return g;
    }

    public static GradientDrawable outlined(Context c,int color,int radius){
        GradientDrawable g=bg(c,color,radius);g.setStroke(dp(c,1),0xff1e4f84);return g;
    }

    public static GradientDrawable subtleSurface(Context c,int radius){
        GradientDrawable g=new GradientDrawable(GradientDrawable.Orientation.TL_BR,new int[]{SURFACE2,SURFACE});
        g.setCornerRadius(dp(c,radius));
        g.setStroke(dp(c,1),0xff173b63);
        return g;
    }

    public static GradientDrawable interactiveSurface(Context c,int radius){
        GradientDrawable g=bg(c,SURFACE3,radius);g.setStroke(dp(c,1),0xff1e4f84);return g;
    }

    private static StateListDrawable buttonBackground(Context c, boolean primary){
        StateListDrawable state=new StateListDrawable();
        state.addState(new int[]{android.R.attr.state_pressed},primary?bg(c,BLUE_PRESSED,18):outlined(c,SURFACE3,18));
        state.addState(new int[]{android.R.attr.state_focused},primary?bg(c,BLUE_PRESSED,18):outlined(c,SURFACE3,18));
        state.addState(new int[]{android.R.attr.state_enabled},primary?bg(c,BLUE,18):outlined(c,SURFACE2,18));
        state.addState(new int[]{},outlined(c,SURFACE,18));
        return state;
    }

    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("tnum");v.setLineSpacing(0,1.08f);return v;
    }

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

    public static Button button(Context c,String s){
        Button b=new Button(c);b.setText(s);b.setTextColor(Color.WHITE);b.setTextSize(15);b.setAllCaps(false);
        b.setTypeface(Typeface.create("sans",Typeface.BOLD));b.setBackground(buttonBackground(c,false));b.setMinHeight(dp(c,52));b.setMinWidth(dp(c,52));
        b.setPadding(dp(c,18),0,dp(c,18),0);b.setStateListAnimator(null);b.setGravity(Gravity.CENTER);b.setLetterSpacing(.01f);return b;
    }

    public static Button primary(Context c,String s){
        Button b=button(c,s);b.setBackground(buttonBackground(c,true));return b;
    }

    public static LinearLayout card(Context c){
        LinearLayout v=new LinearLayout(c);v.setOrientation(LinearLayout.VERTICAL);v.setPadding(dp(c,20),dp(c,18),dp(c,20),dp(c,18));
        v.setBackground(subtleSurface(c,22));v.setClipToOutline(true);v.setElevation(0);
        LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(-1,-2);p.setMargins(0,dp(c,10),0,dp(c,10));v.setLayoutParams(p);return v;
    }

    public static LinearLayout sectionHeader(Context c,String heading,String subtitle){
        LinearLayout box=new LinearLayout(c);box.setOrientation(LinearLayout.VERTICAL);box.setPadding(0,dp(c,4),0,dp(c,10));
        box.addView(title(c,heading,25));
        if(subtitle!=null&&!subtitle.isEmpty()){TextView sub=text(c,subtitle,14,SILVER);sub.setPadding(0,dp(c,3),0,0);box.addView(sub);}
        return box;
    }

    public static LinearLayout emptyState(Context c,String heading,String subtitle){
        LinearLayout box=new LinearLayout(c);box.setOrientation(LinearLayout.VERTICAL);box.setGravity(Gravity.CENTER);
        box.setPadding(dp(c,24),dp(c,42),dp(c,24),dp(c,42));box.setBackground(subtleSurface(c,22));
        TextView icon=title(c,"◷",34);icon.setTextColor(BLUE);icon.setGravity(Gravity.CENTER);
        box.addView(icon,new LinearLayout.LayoutParams(-1,dp(c,52)));
        TextView h=title(c,heading,18);h.setGravity(Gravity.CENTER);box.addView(h);
        TextView sub=text(c,subtitle,14,SILVER);sub.setGravity(Gravity.CENTER);sub.setPadding(0,dp(c,8),0,0);box.addView(sub);
        return box;
    }

    public static void setNavSelected(Context c,Button b,boolean selected){
        GradientDrawable g=selected?bg(c,BLUE_SOFT,16):bg(c,Color.TRANSPARENT,16);
        if(selected) g.setStroke(dp(c,1),BLUE);
        b.setBackground(g);b.setTextColor(selected?BLUE:SILVER);b.setAlpha(selected?1f:.96f);b.setSelected(selected);
    }

    public static TextView badge(Context c,String value){
        TextView v=text(c,value,12,BLUE);v.setGravity(Gravity.CENTER);v.setTypeface(Typeface.create("sans",Typeface.BOLD));
        v.setPadding(dp(c,12),0,dp(c,12),0);v.setBackground(bg(c,BLUE_SOFT,14));return v;
    }

    public static void applySystemSafeInsets(android.app.Activity activity, View root){
        final int l=root.getPaddingLeft(),t=root.getPaddingTop(),r=root.getPaddingRight(),b=root.getPaddingBottom();
        if(android.os.Build.VERSION.SDK_INT>=35){
            try{activity.getWindow().setDecorFitsSystemWindows(false);}catch(Throwable ignored){}
        }
        root.setOnApplyWindowInsetsListener((v,insets)->{
            android.graphics.Insets bars=insets.getInsets(
                    android.view.WindowInsets.Type.statusBars()|android.view.WindowInsets.Type.navigationBars());
            android.graphics.Insets cutout=insets.getInsets(android.view.WindowInsets.Type.displayCutout());
            int safeLeft=Math.max(bars.left,cutout.left);
            int safeTop=Math.max(bars.top,cutout.top);
            int safeRight=Math.max(bars.right,cutout.right);
            int safeBottom=Math.max(bars.bottom,cutout.bottom);
            v.setPadding(l+safeLeft,t+safeTop,r+safeRight,b+safeBottom);
            return insets;
        });
        root.requestApplyInsets();
    }


    public static Button premiumIconButton(Context c,String description){
        Button b=new Button(c);
        b.setText("");
        b.setContentDescription(description);
        b.setMinWidth(dp(c,48));
        b.setMinHeight(dp(c,48));
        b.setPadding(dp(c,13),dp(c,13),dp(c,13),dp(c,13));
        b.setGravity(Gravity.CENTER);
        b.setStateListAnimator(null);
        b.setBackground(premiumIconButtonBackground(c));
        b.setCompoundDrawableTintList(new android.content.res.ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_pressed},
                        new int[]{android.R.attr.state_focused},
                        new int[]{}
                },
                new int[]{
                        Color.rgb(103,166,255),
                        Color.rgb(103,166,255),
                        BLUE
                }));
        return b;
    }

    private static Drawable premiumIconButtonBackground(Context c){
        android.graphics.drawable.StateListDrawable state=new android.graphics.drawable.StateListDrawable();
        state.addState(new int[]{android.R.attr.state_pressed},outlined(c,Color.rgb(16,38,65),24));
        state.addState(new int[]{android.R.attr.state_focused},outlined(c,Color.rgb(11,25,41),24));
        state.addState(new int[]{},outlined(c,Color.rgb(7,17,30),24));
        return state;
    }

    public static Button iconButton(Context c,String glyph,String description){
        Button b=button(c,glyph);b.setTextSize(22);b.setContentDescription(description);
        b.setBackground(interactiveSurface(c,18));b.setPadding(0,0,0,0);return b;
    }

    public static LinearLayout subpageTopBar(android.app.Activity a,String title){
        LinearLayout bar=new LinearLayout(a);bar.setGravity(Gravity.CENTER_VERTICAL);
        Button back=iconButton(a,"","Back");back.setCompoundDrawablesWithIntrinsicBounds(0,0,com.lemenzo.clock.R.drawable.ic_back,0);back.setOnClickListener(v->a.finish());
        bar.addView(back,new LinearLayout.LayoutParams(dp(a,48),dp(a,48)));
        TextView heading=title(a,title,23);heading.setPadding(dp(a,14),0,0,0);
        bar.addView(heading,new LinearLayout.LayoutParams(0,dp(a,56),1));
        return bar;
    }


    public static GradientDrawable blueOutline(Context c,int radius){
        GradientDrawable g=bg(c,SURFACE,radius);g.setStroke(dp(c,1),0xff1d4f86);return g;
    }

    public static GradientDrawable navBarBackground(Context c){
        GradientDrawable g=bg(c,SURFACE,24);
        g.setStroke(dp(c,1),0xff1e4f84);
        return g;
    }

    public static LinearLayout premiumCard(Context c){
        LinearLayout v=card(c);
        v.setBackground(subtleSurface(c,22));
        v.setElevation(dp(c,1));
        return v;
    }

    public static TextView eyebrow(Context c,String s){
        TextView v=text(c,s,12,BLUE);v.setTypeface(Typeface.create("sans",Typeface.BOLD));
        v.setLetterSpacing(.08f);return v;
    }

    public static TextView heroNumber(Context c,String s,int sp){
        TextView v=title(c,s,sp);v.setGravity(Gravity.CENTER);v.setFontFeatureSettings("tnum");
        return v;
    }

    public static Button navButton(Context c,String label,int iconRes){
        Button b=button(c,label);b.setTextSize(10);b.setTypeface(Typeface.create("sans",Typeface.NORMAL));
        b.setSingleLine(true);b.setEllipsize(android.text.TextUtils.TruncateAt.END);
        b.setPadding(dp(c,4),dp(c,5),dp(c,4),dp(c,4));
        b.setCompoundDrawablesWithIntrinsicBounds(0,iconRes,0,0);b.setCompoundDrawablePadding(dp(c,2));
        return b;
    }

    public static void crossFade(android.view.View view,Runnable replace){
        if(view==null){replace.run();return;}
        view.animate().alpha(0f).setDuration(110).withEndAction(()->{
            replace.run();
            view.setAlpha(0f);
            view.animate().alpha(1f).setDuration(150).start();
        }).start();
    }

    public static LinearLayout row(Context c){
        LinearLayout row=new LinearLayout(c);row.setGravity(Gravity.CENTER_VERTICAL);
        row.setPadding(dp(c,16),dp(c,8),dp(c,12),dp(c,8));row.setBackground(subtleSurface(c,16));
        return row;
    }

    public static TextView value(Context c,String s){
        TextView v=text(c,s,14,SILVER);v.setGravity(Gravity.END|Gravity.CENTER_VERTICAL);return v;
    }


    public static EditText input(Context c,String hint){
        EditText e=new EditText(c);e.setHint(hint);e.setSingleLine();
        e.setTextColor(WHITE);e.setHintTextColor(SILVER);e.setTextSize(15);
        e.setBackground(interactiveSurface(c,18));e.setPadding(dp(c,16),dp(c,13),dp(c,16),dp(c,13));
        e.setMinHeight(dp(c,54));
        e.setSelectAllOnFocus(false);return e;
    }

    public static Switch toggle(Context c,String label,boolean checked){
        Switch s=new Switch(c);s.setText(label);s.setTextColor(WHITE);s.setTextSize(15);
        s.setChecked(checked);s.setMinimumHeight(dp(c,52));s.setPadding(0,dp(c,4),0,dp(c,4));
        try{
            s.setThumbTintList(new ColorStateList(
                    new int[][]{new int[]{android.R.attr.state_checked},new int[]{}},
                    new int[]{BLUE,0xff86abd8}));
            s.setTrackTintList(new ColorStateList(
                    new int[][]{new int[]{android.R.attr.state_checked},new int[]{}},
                    new int[]{0xff2f66a8,0xff102a4b}));
        }catch(Throwable ignored){}
        return s;
    }

    public static void styleDialog(android.app.AlertDialog d){
        if(d==null)return;
        android.view.Window w=d.getWindow();
        if(w!=null){
            w.setBackgroundDrawable(bg(d.getContext(),SURFACE,22));
            try{w.setDimAmount(.55f);}catch(Throwable ignored){}
        }
        try{
            TextView title=d.findViewById(d.getContext().getResources().getIdentifier("alertTitle","id","android"));
            if(title!=null)title.setTextColor(WHITE);
        }catch(Throwable ignored){}
        for(int which:new int[]{android.app.AlertDialog.BUTTON_POSITIVE,android.app.AlertDialog.BUTTON_NEGATIVE,android.app.AlertDialog.BUTTON_NEUTRAL}){
            Button b=d.getButton(which);if(b!=null){b.setTextColor(BLUE);b.setAllCaps(false);}
        }
    }

    public static android.app.AlertDialog showStyled(android.app.AlertDialog dialog){
        dialog.setOnShowListener(x->styleDialog(dialog));dialog.show();return dialog;
    }

    public static TextView cityRow(Context c,String label){
        TextView v=text(c,label,15,WHITE);v.setPadding(dp(c,16),dp(c,14),dp(c,16),dp(c,14));
        v.setBackground(subtleSurface(c,14));return v;
    }


    public static Button destructive(Context c,String s){
        Button b=button(c,s);b.setTextColor(DANGER);b.setBackground(bg(c,0xff241116,18));return b;
    }

    public static void styleDayChip(Context c, android.widget.CheckBox chip){
        chip.setButtonDrawable(null);
        chip.setGravity(Gravity.CENTER);
        chip.setTextSize(14);
        chip.setTypeface(Typeface.create("sans",Typeface.BOLD));
        chip.setMinHeight(dp(c,48));
        chip.setPadding(dp(c,8),0,dp(c,8),0);
        chip.setTextColor(chip.isChecked()?WHITE:SILVER);
        GradientDrawable g=bg(c,chip.isChecked()?BLUE_SOFT:SURFACE2,16);
        g.setStroke(dp(c,1),chip.isChecked()?BLUE:0xff1e4f84);
        chip.setBackground(g);
    }

    public static LinearLayout settingsRow(Context c,String titleText,String valueText){
        LinearLayout row=new LinearLayout(c);row.setOrientation(LinearLayout.VERTICAL);
        row.setPadding(dp(c,16),dp(c,13),dp(c,16),dp(c,13));row.setBackground(interactiveSurface(c,18));
        TextView title=title(c,titleText,15);row.addView(title,new LinearLayout.LayoutParams(-1,-2));
        if(valueText!=null&&!valueText.isEmpty()){TextView value=text(c,valueText,13,BLUE_BRIGHT);value.setPadding(0,dp(c,4),0,0);row.addView(value,new LinearLayout.LayoutParams(-1,-2));}
        row.setClickable(true);row.setFocusable(true);row.setMinimumHeight(dp(c,62));return row;
    }

    public static TextView sectionLabel(Context c,String s){
        TextView v=eyebrow(c,s);v.setTextColor(BLUE_BRIGHT);v.setPadding(dp(c,2),dp(c,4),0,dp(c,8));return v;
    }

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