{ config, lib, pkgs, ... }:

{
  imports = [
    ./modules/hyprland.nix
  ];
  # Home Manager needs a bit of information about you and the
  # paths it should manage.
  home.username = "chris";
  home.homeDirectory = "/home/chris";

  # This value determines the Home Manager release that your
  # configuration is compatible with. This helps avoid breakage
  # when a new Home Manager release introduces backwards
  # incompatible changes.
  #
  # You can update Home Manager without changing this value. See
  # the Home Manager release notes for a list of state version
  # changes in each release.
  home.stateVersion = "21.11"; # Did you read the comment?

  # Let Home Manager install and manage itself.
  programs.home-manager.enable = true;

  xdg.userDirs = {
    enable = true;
    desktop = "${config.home.homeDirectory}";
    documents = "${config.home.homeDirectory}/docs";
    download = "${config.home.homeDirectory}/dls";
    music = "${config.home.homeDirectory}/music";
    pictures = "${config.home.homeDirectory}/pics";
    publicShare = "${config.home.homeDirectory}";
    templates = "${config.home.homeDirectory}";
    videos = "${config.home.homeDirectory}/vids";
  };

  programs.gpg = {
    enable = true;
  };

  services.gpg-agent = {
    enable = true;
    pinentryFlavor = "qt";
    # extraConfig = ''
    #   pinentry-program ${pkgs.pinentry.qt}/bin/pinentry
    # '';
  };

  accounts.email = {
    maildirBasePath = "mail";
    accounts = {
      personal = {
        address = "chris@cochrun.xyz";
        userName = "chris@cochrun.xyz";
        mbsync.enable = true;
        mu.enable = true;
        flavor = "plain";
        primary = true;
        passwordCommand = "${pkgs.rbw}/bin/rbw get 'Office 365'";
        realName = "Chris Cochrun";
        signature = {
          text = ''
          Praising God in all things,
          Chris Cochrun
          '';
          delimiter = ''
          ***
          '';
          showSignature = "append";
        };
        imap = {
          host = "mail.cochrun.xyz";
          port = 993;
          tls.enable = true;
        };
        smtp = {
          host = "mail.cochrun.xyz";
          port = 25;
          tls.enable = true;
        };
        mbsync = {
          create = "maildir";
          remove = "both";
          expunge = "both";
        };
        maildir.path = "cochrun";
      };

      work = {
        address = "chris@tfcconnection.org";
        userName = "chris@tfcconnection.org";
        mbsync.enable = true;
        mu.enable = true;
        flavor = "outlook.office365.com";
        passwordCommand = "cat ~/.config/access";
        realName = "Chris Cochrun";
        imap = {
          host = "outlook.office365.com";
          port = 993;
          tls.enable = true;
        };
        smtp = {
          host = "smtp.office365.com";
          port = 587;
          tls.enable = true;
        };
        mbsync = {
          create = "maildir";
          remove = "both";
          expunge = "both";
          extraConfig.account = {
            AuthMechs = "XOAUTH2";
          };
        };
        maildir.path = "office";
        signature = {
          text = ''
          Praising God in all things,
          Chris Cochrun
          '';
          delimiter = ''
          ***
          '';
          showSignature = "append";
        };
      };
    };
  };

  programs.git = {
    enable = true;
    userName = "Chris Cochrun";
    userEmail = "chris@cochrun.xyz";
  };

  home.packages = with pkgs; [
  ];

  programs.mu.enable = true;

  programs.mbsync = {
    package = pkgs.isync.override { withCyrusSaslXoauth2 = true; };
    enable = true;
  };
  programs.msmtp.enable = true;
  services.mbsync.enable = true;

  programs.obs-studio = {
    enable = true;
    plugins = [ pkgs.obs-studio-plugins.obs-move-transition ];
  };

  home.file.".config/mpv" = {
    source = ../.config/mpv;
  };

  # services.kdeconnect.enable = true;

  services = {
    syncthing.enable = true;
    easyeffects.enable = true;
    nextcloud-client = {
      enable = true;
      startInBackground = true;
    };

    mpd.enable = true;
    pueue.enable = true;

    # For matrix clients that don't have e2ee
    pantalaimon = {
      enable = true;
      settings = {
        Default = {
          LogLevel = "Debug";
          SSL = true;
        };
        local-matrix = {
          Homeserver = "https://matrix.tfcconnection.org";
          ListenAddress = "127.0.0.1";
          ListenPort = 8008;
          # SSL = true;
          # IgnoreVerification = true;
        };
      };
    };
  };


 #services.espanso = {
 #  enable = true;
 #  settings = {
 #    toggle_key = "RIGHT_CTRL";
 #    matches = [
 #      { # dates
 #        trigger = ":date";
 #        replace = "{{mydate}}";
 #        vars = [{

 #          name = "mydate";
 #          type = "date";
 #          params = {format = "%m/%d/%Y";};
 #        }];
 #      }
 #      { # Shell commands
 #        trigger = ":shell";
 #        replace = "{{output}}";
 #        vars = [{
 #          name = "output";
 #          type = "shell";
 #          params = { cmd = "echo Hello from your shell";};
 #        }];
 #      }
 #      { # simple text
 #        trigger = ":gml";
 #        replace = "ccochrun21@gmail.com";
 #      }
 #      {
 #        trigger = ":otl";
 #        replace = "chris.cochrun@outlook.com";
 #      }
 #      {
 #        trigger = ":tfcml";
 #        replace = "chris@tfcconnection.org";
 #      }
 #      {
 #        trigger = ":name";
 #        replace = "Chris Cochrun";
 #      }
 #      {
 #        trigger = ":cn";
 #        replace = "A Giant Gummy Lizard";
 #      }
 #    ];
 #  };
 #};

  home.file.".config/bottom" = {
    source = ../.config/bottom;
    recursive = true;
  };

  home.file.".config/rofi" = {
    source = ../.config/rofi;
    recursive = true;
  };

  programs.firefox = {
    enable = true;
    package = pkgs.firefox-wayland.override {
      nativeMessagingHosts = [
        pkgs.plasma-browser-integration
        pkgs.tridactyl-native
      ];
      # cfg = {
      #   enableTridactylNative = true;
      #   enablePlasmaBrowserIntegration = true;
      # };
    };
    profiles.chris = {
      name = "default";
      path = "nw77o6yc.default";
      isDefault = true;
    };
  };

  programs.waybar = {
    enable = true;
    settings = {
      mainBar = {
        layer = "top";
        output = ["DP-1" "eDP-1"];
        position = "bottom";
        height = 35;
        width = 1400;
        # Choose the order of the modules "custom/wintitle", 
        modules-left = ["wlr/workspaces" "workspaces" "hyprland/window"];
        modules-center = ["clock"];
        modules-right = ["pulseaudio" "backlight" "temperature" "memory" "cpu" "battery" "tray"];
        margin-top = -5;
        margin-bottom = 8;
        margin-left = 6;
        margin-right = 6;
        "hyprland/window" = {
          format = "  <span style='italic'>{}</span>";
        };
        workspaces = {
          format = " {name} ";
        };
        "wlr/workspaces" = {
          format = "{icon}";
          on-scroll-up = "hyprctl dispatch workspace e+1";
          on-scroll-down = "hyprctl dispatch workspace e-1";
        };
        tray = {
          icon-size = 21;
          spacing = 10;
        };
        clock = {
          tooltip-format = "<big>{:%Y %B}</big>\n<tt><small>{calendar}</small></tt>";
          format-alt = "{:%Y-%m-%d}";
          format = "{:%a %b %e, %I:%M %p}";
        };
        cpu = {
          format = "  {usage}%";
        };
        memory = {
          format = "  {}%";
        };
        temperature = {
          # "thermal-zone": 2,
          # "hwmon-path": "/sys/class/hwmon/hwmon2/temp1_input",
          critical-threshold = 80;
          # "format-critical": "{temperatureC}°C {icon}",
          format = "{icon} {temperatureC}°C";
          format-icons = ["" "" ""];
        };
        backlight = {
          # "device": "acpi_video1",
          format = "{icon}  {percent}%";
          format-icons = ["" ""];
        };
        battery = {
          states = {
            good = 95;
            warning = 30;
            critical = 15;
          };
          format = "{icon} {capacity}%";
          format-charging = "{icon} {capacity}%";
          format-plugged = " {capacity}%";
          format-alt = "{time} {icon}";
          format-good = "{icon} {capacity}%";
          format-full = "󰁹 {capacity}%";
          format-icons = ["󰁺" "󰁼" "󰁾" "󰂀" "󰂂"];
          format-charging-icons = ["󰢜" "󰂇" "󰢝" "󰢞" "󰂋"];
        };
        network = {
          # "interface": "wlp2*", // (Optional) To force the use of this interface
          format-wifi = "  ({signalStrength}%)";
          format-ethernet = " {ipaddr}";
          format-linked = "{ifname} (No IP) ";
          format-disconnected = "Disconnected ⚠";
          format-alt = "{ifname}: {essid} {ipaddr}/{cidr}";
        };
        pulseaudio = {
          scroll-step = 1;
          format = "{icon} {volume}% {format_source}";
          format-bluetooth = "{icon} {volume}% {format_source}";
          format-bluetooth-muted = " {icon} {format_source}";
          format-muted = "ﱝ muted";
          format-source = " {volume}%";
          format-source-muted = "";
          format-icons = {
            headphone = "";
            hands-free = "";
            headset = "";
            phone = "";
            portable = "";
            car = "";
            default = ["" "" ""];
          };
          on-click = "alacritty --class pulsemixer -e pulsemixer";
        };
      };
    };
    style = ''
@define-color base00 #282a36;
@define-color base01 #34353e;
@define-color base02 #43454f;
@define-color base03 #78787e;
@define-color base04 #a5a5a9;
@define-color base05 #e2e4e5;
@define-color base06 #eff0eb;
@define-color base07 #f1f1f0;
@define-color base08 #ff5c57;
@define-color base09 #ff9f43;
@define-color base0A #f3f99d;
@define-color base0B #5af78e;
@define-color base0C #9aedfe;
@define-color base0D #57c7ff;
@define-color base0E #ff6ac1;
@define-color base0F #b2643c;
@define-color basetransparent rgba(40, 42, 54, 0.0);
@define-color backtransparent rgba(40, 42, 54, 0.7);
* {
    border:        1px;
    border-radius: 20px;
    font-family:   VictorMono Nerd Font;
    font-size:     15px;
    font-weight:   normal;
    box-shadow:    none;
    text-shadow:   none;
    transition-duration: 0s;
    padding-top: 0px;
    padding-bottom: 0px;
}


window {
    background: transparent;
    /* border-radius: 20px; */
}

window#waybar > box {
    padding-top: 5px;
    padding-bottom: 3px;
    padding-left: 3px;
    padding-right: 10px;
    color:      @base05;
    box-shadow: 5px 5px 3px 3px #101010;
    margin: 12px 14px 14px 14px;
    background: @backtransparent;
}

#workspaces {
    border-radius: 20px;
    margin-left: 10px;
    padding-right: 10px;
    background: transparent;
    transition: none;
}

#tags {
    border-radius: 20px;
    margin-left: 6px;
    padding-right: 10px;
    background: transparent;
    transition: none;
}

#workspaces button {
    transition: none;
    color:      rgba(217, 216, 216, 0.4);
    background: transparent;
}

#tags button {
    transition: none;
    color:      rgba(217, 216, 216, 0.4);
    background: transparent;
}

#workspaces button.visible {
    color:      rgba(217, 216, 216, 1);
}

#tags button.occupied {
    color:      rgba(217, 216, 216, 1);
}

#workspaces button.focused {
    color: @base0C;
}

#tags button.focused {
    color: @base0C;
}

#workspaces button:hover {
    transition: none;
    box-shadow: inherit;
    text-shadow: inherit;
    color: @base0E;
}

#workspaces button.urgent {
    color:      @base08;
}

#mode, #battery, #cpu, #memory, #network, #pulseaudio, #idle_inhibitor, #backlight, #custom-storage, #custom-spotify, #custom-weather, #custom-mail {
    margin:     0px 3px 0px 3px;
    /* min-width:  25px; */
}

#clock {
    margin-left: 2px;
    margin-right: 2px;
    padding-left: 10px;
    padding-right: 10px;
    border-radius: 20px;
    transition: none;
    color: @base0B;
    background: transparent;
}

#backlight {
    margin-left: 2px;
    margin-right: 2px;
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base08;
    background: transparent;
}

#battery.warning {
    color:       @base09;
}

#battery.critical {
    color:      @base08;
}

#battery.charging {
    color:      @base0B;
}

#pulseaudio {
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base09;
    background: transparent;
}

#network {
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base0B;
    background: transparent;
}

#cpu {
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base09;
    background: transparent;
}

#battery {
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base0D;
    background: transparent;
}

#battery.bat2 {
    margin-right: 10px;
}

#memory {
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base0C;
    background: transparent;
}

#temperature {
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base0E;
    background: transparent;
}

#tray {
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 20px;
    transition: none;
    color: @base0E;
    background: transparent;
}

    '';
  };

  home.file.".mozilla/native-messaging-hosts".source = "/run/current-system/sw/lib/mozilla/native-messaging-hosts";

  home.file.".mozilla/firefox/nw77o6yc.default/chrome" = {
    source = ../.config/firefox/chrome;
    recursive = true;
  };

  home.file.".config/tridactyl" = {
    source = ../.config/tridactyl;
    recursive = true;
  };

  programs.rbw.enable = true;
  home.file.".config/rbw" = {
    source = ../.config/rbw;
    recursive = true;
  };

  home.file.".config/fish/config.fish" = {
    source = ../.config/fish/config.fish;
  };

  home.file.".config/fish/functions" = {
    source = ../.config/fish/functions;
  };

  programs.nushell = {
    enable = true;
    # package = pkgs.nushellFull;
    shellAliases = {
      # ls = "eza -l";
      la = "ls -la";
      mpf = "mpv --profile=fast";
      mps = "mpv --profile=slow";
      ec = "emacsclient -t";
      ecc = "emacsclient -c";
      # mkdir = "mkdir -p";
      nupd = "update-nix";
      nupg = "upgrade-nix";
      suspend = "systemctl suspend";
      sysuse = "systemctl --user";
      myip = "curl icanhazip.com";
      nixs = "nix search nixpkgs";
      ytd = "yt-dlp -o '~/Videos/%(title)s.%(ext)s'";
    };
    configFile = {
      text = ''
      $env.config = {
        show_banner: false
      }
      '';
    };
    environmentVariables = {
      EDITOR = "\"emacsclient -t\"";
    };
  };

  home.file.".config/dunst" = {
    source = ../.config/dunst;
  };


  programs.atuin.enable = true;

  programs.direnv = {
    enable = true;
    nix-direnv.enable = true;
  };

  # programs.qutebrowser.enable = true;
  home.file.".config/qutebrowser/config.py" = {
    source = ../.config/qutebrowser/config.py;
  };

  home.file.".config/qutebrowser/bookmarks" = {
    source = ../.config/qutebrowser/bookmarks;
  };

  home.file.".config/qutebrowser/css" = {
    source = ../.config/qutebrowser/css;
  };

  home.file.".config/qutebrowser/quickmarks" = {
    source = ../.config/qutebrowser/quickmarks;
  };

  home.file.".config/qutebrowser/qsettings" = {
    source = ../.config/qutebrowser/qsettings;
  };

  home.file.".config/imv" = {
    source = ../.config/imv;
    recursive = true;
  };

  home.file.".config/macchina" = {
    source = ../.config/macchina;
    recursive = true;
  };

 #home.file.".config/awesome" = {
 #  source = ../awesome;
 #  recursive = true;
 #};

  home.file.".config/awesome/bling" = {
    source = ../.config/awesome/bling;
    recursive = true;
  };

  home.file.".config/awesome/rubato" = {
    source = ../.config/awesome/rubato;
    recursive = true;
  };

  home.file.".config/alacritty" = {
    source = ../.config/alacritty;
  };

  home.file.".config/picom.conf" = {
    source = ../.config/picom.conf;
  };

  # home.file.".config/networkmanager-dmenu/config.ini" = {
  #   source = ../networkmanager-dmenu/config.ini;
  # };

  home.file."bin" = {
    source = ../scripts;
    recursive = true;
  };

  home.shellAliases = {
    ls = "eza -l";
    la = "eza -la";
    mpf = "mpv --profile=fast";
    mps = "mpv --profile=slow";
    ec = "emacsclient -t";
    ecc = "emacsclient -c";
    mkdir = "mkdir -pv";
    nupd = "update-nix";
    nupg = "upgrade-nix";
    suspend = "systemctl suspend";
    sysuse = "systemctl --user";
    myip = "curl icanhazip.com";
    nixs = "nix search nixpkgs";
    ytd = "yt-dlp -o \"~/vids/%(title)s.%(ext)s\" $1";
  };

  programs.starship = {
    enable = true;
    enableNushellIntegration = true;
    enableBashIntegration = true;
    settings = {
      format = ''
        \[(dimmed gray) $username$hostname$directory\](dimmed gray)
        $character
      '';
      right_format = ''
        $git_branch$git_status$rust$nix_shell
      '';
      scan_timeout = 10;
      rust = {
        format = "[$symbol($version )]($style)";
      };
      cmake = {
        format = "";
        disabled = true;
      };
      nix_shell = {
        format = "[$symbol$state( \($name\))]($style) ";
        impure_msg = " ";
        pure_msg = " ";
      };
      hostname = {
        format = "@[$ssh_symbol$hostname]($style) > '";
      };
      git_branch = {
        format = "[$symbol$branch(:$remote_branch)]($style) ";
      };
      git_status = {
        format = "[\\[$all_status$ahead_behind\\]]($style) |(dimmed gray) ";
      };
      character = {
        success_symbol = "[󰄾](bold green)";
        error_symbol = "[x](bold red)";
      };
    };
  };

  programs.bash = {
    enable = true;
    bashrcExtra = ''
    # export ENV_EFI_CODE_SECURE=/run/libvirt/nix-ovmf/OVMF_CODE.fd ENV_EFI_VARS_SECURE=/run/libvirt/nix-ovmf/OVMF_VARS.fd
    case "$-" in
      *i*)
      source $(blesh-share)/ble.sh
      ble-face auto_complete="fg=238"
      eval "$(starship init bash)"
      macchina;;
    esac
    # export LESS_TERMCAP_mb=$'\e[1;32m'
    # export LESS_TERMCAP_md=$'\e[1;32m'
    # export LESS_TERMCAP_me=$'\e[0m'
    # export LESS_TERMCAP_se=$'\e[0m'
    # export LESS_TERMCAP_so=$'\e[01;33m'
    # export LESS_TERMCAP_ue=$'\e[0m'
    # export LESS_TERMCAP_us=$'\e[1;4;31m'
    '';
  };

  programs.zsh = {
    enable = true;
    enableAutosuggestions = true;
    enableCompletion = true;
    syntaxHighlighting.enable = true;
    autocd = true;
    dotDir = ".config/zsh";
    shellAliases = {
      ls = "eza -l";
      la = "eza -la";
      mpf = "mpv --profile=fast";
      mps = "mpv --profile=slow";
      ec = "emacsclient -t";
      ecc = "emacsclient -c";
      mkdir = "mkdir -pv";
      nupd = "update-nix";
      nupg = "upgrade-nix";
      suspend = "systemctl suspend";
      sysuse = "systemctl --user";
      myip = "curl icanhazip.com";
      ytd = "yt-dlp -o \"~/vids/%(title)s.%(ext)s\" $1";
    };
    initExtra = ''
      macchina
    '';
  };

  xdg.desktopEntries = {
    mpv-slow = {
      name = "MPV";
      genericName = "Play from MPV but at normal speed";
      exec = "alacritty -e mpv --profile=slow %U";
      terminal = true;
      categories = [ "Application" ];
      mimeType = [ "audio/ogg" "audio/mpeg" "audio/opus" "audio/x-opus+ogg" "audio/x-wav" ];
    };
    imv-rifle = {
      name = "IMV";
      genericName = "Show images in current directory in IMV";
      exec = "/home/chris/bin/rifle-imv %U";
      terminal = false;
      categories = [ "Application" ];
      mimeType = [ "image/gif" "image/jpeg" "image/png" "image/heif" ];
    };
  };

  systemd.user.services = {
    ydotoold = {
      Unit = {
        Description = "An auto-input utility for wayland";
        Documentation = [ "man:ydotool(1)" "man:ydotoold(8)" ];
      };
      
      Service = {
        ExecStart = "/run/current-system/sw/bin/ydotoold --socket-path /tmp/ydotools";
      };

      Install = {
        WantedBy = ["default.target"];
      };
    };

    jellyfin-mpv-shim = {
      Unit = {
        Description = "Play Jellyfin media in mpv";
        After = "graphical-session-pre.target";
      };
      
      Service = {
        ExecStart = "/run/current-system/sw/bin/jellyfin-mpv-shim";
      };

      Install = {
        WantedBy = ["graphical-session.target"];
      };
    };

    nextlcoud-client = {
      Unit = {
        Description = "Nextcloud Client";
        After = [ "graphical-session.target" ];
        # PartOf = [ "plasma-workspace.target" ];
      };
      Service = {
        Environment = ["PATH=/etc/profiles/per-user/chris/bin"
                       "WAYLAND_DISPLAY=wayland-1"];
      };
    };
  };

}