{ 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.desktop = "${config.home.homeDirectory}/desk";
  xdg.userDirs.documents = "${config.home.homeDirectory}/docs";
  xdg.userDirs.download = "${config.home.homeDirectory}/dls";
  xdg.userDirs.music = "${config.home.homeDirectory}/music";
  xdg.userDirs.pictures = "${config.home.homeDirectory}/pics";
  xdg.userDirs.publicShare = "${config.home.homeDirectory}/public";
  # xdg.userDirs.temptlates = "${config.home.homeDirectory}/templates";
  xdg.userDirs.videos = "${config.home.homeDirectory}/vids";

  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 = "/home/chris/.dotfiles/scripts/mailpass";
        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 = "LOGIN";
          };
        };
        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 = {
    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.nextcloud-client = {
    enable = true;
    startInBackground = true;
  };

  services.syncthing.enable = true;
  # services.kdeconnect.enable = true;
  services.easyeffects.enable = true;

  # For matrix clients that don't have e2ee
  services.pantalaimon = {
    enable = true;
    settings = {
      Default = {
        LogLevel = "Debug";
        SSL = true;
      };
      local-matrix = {
        Homeserver = "https://matrix.tfcconnection.org";
        ListenAddress = "localhost";
        ListenPort = 8008;
        SSL = false;
        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/rofi" = {
    source = ../.config/rofi;
    recursive = true;
  };

  programs.firefox = {
    enable = true;
    package = pkgs.firefox-wayland.override {
      cfg = {
        enableTridactylNative = true;
        enablePlasmaBrowserIntegration = true;
      };
    };
    profiles.chris = {
      name = "default";
      path = "nw77o6yc.default";
      isDefault = true;
    };
  };

  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;
    shellAliases = {
      # ls = "exa -l";
      la = "exa -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'";
    };
    extraConfig = ''
      let-env config = {
        show_banner: false
      }
    '';
  };

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

  programs.direnv.enable = true;
  programs.direnv.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 = "exa -l";
    la = "exa -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 '~/Videos/%(title)s.%(ext)s'";
  };

  programs.starship = {
    enable = 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) ";
      };
    };
  };

  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
    source $(blesh-share)/ble.sh
    ble-face auto_complete="fg=238"
    # 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'
    eval "$(starship init bash)"
    '';
  };

  programs.zsh = {
    enable = true;
    enableAutosuggestions = true;
    enableCompletion = true;
    enableSyntaxHighlighting = true;
    autocd = true;
    dotDir = ".config/zsh";
    shellAliases = {
      ls = "exa -l";
      la = "exa -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";
    };
    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"];
      };
    };
  };

}