{ 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;

  # accounts.email.accounts = {
  #   personal = {
  #     address = "chris@cochrun.xyz";
  #     mbsync.enable = true;
  #     create =  "maildir";
  #     mu.enable = true;
  #     imap = {
  #       host = "mail.cochrun.xyz";
  #       port = 993;
  #       tls.enable = true;
  #     };
  #     smtp = {
  #       host = "mail.cochrun.xyz";
  #       port = 25;
  #       tls.enable = true;
  #     };
  #   };
  #   work = {
  #     address = "chris@tfcconnection.org";
  #     mbsync.enable = true;
  #     create =  "maildir";
  #     mu.enable = true;
  #     imap = {
  #       host = "outlook.office365.com";
  #       port = 993;
  #       tls.enable = true;
  #     };
  #     smtp = {
  #       host = "mail.cochrun.xyz";
  #       port = 25;
  #       tls.enable = true;
  #     };
  #   };
  # };

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

  home.packages = with pkgs; [
  ];

  programs.mu.enable = true;

  programs.mbsync = {
    enable = true;
    extraConfig = ''
    IMAPAccount gmail
    Host imap.gmail.com
    User ccochrun21@gmail.com
    PassCmd "rbw get gmail"
    AuthMechs LOGIN
    SSLType IMAPS
    SSLVersions SSLv3
    # CertificateFile /opt/local/share/curl/curl-ca-bundle.crt
    
    IMAPAccount office
    Host outlook.office365.com
    User chris@tfcconnection.org
    PassCmd "rbw get 'Office 365'"
    AuthMechs LOGIN
    SSLType IMAPS
    
    IMAPAccount outlook
    Host outlook.office365.com
    User chris.cochrun@outlook.com
    PassCmd "rbw get outlook"
    AuthMechs LOGIN
    SSLType IMAPS
    
    IMAPAccount cochrun
    Host mail.cochrun.xyz
    User chris@cochrun.xyz
    PassCmd "rbw get 'Office 365'"
    AuthMechs LOGIN
    SSLType IMAPS
    # SSLVersions SSLv3
    # CertificateFile /opt/local/share/curl/curl-ca-bundle.crt
    # THEN WE SPECIFY THE LOCAL AND REMOTE STORAGE
    # - THE REMOTE STORAGE IS WHERE WE GET THE MAIL FROM (E.G., THE
    #   SPECIFICATION OF AN IMAP ACCOUNT)
    # - THE LOCAL STORAGE IS WHERE WE STORE THE EMAIL ON OUR COMPUTER
    
    # REMOTE STORAGE (USE THE IMAP ACCOUNT SPECIFIED ABOVE)
    IMAPStore gmail-remote
    Account gmail
    
    MaildirStore gmail-local
    Path ~/Maildir/gmail/
    Inbox ~/Maildir/gmail/INBOX
    
    IMAPStore office-remote
    Account office
    
    # LOCAL STORAGE (CREATE DIRECTORIES with mkdir -p Maildir/gmail)
    
    MaildirStore office-local
    Path ~/Maildir/office/
    Inbox ~/Maildir/office/INBOX
    Subfolders Verbatim
    
    
    IMAPStore outlook-remote
    Account outlook
    
    MaildirStore outlook-local
    Path ~/Maildir/outlook/
    Inbox ~/Maildir/outlook/INBOX
    Subfolders Verbatim
    
    IMAPStore cochrun-remote
    Account cochrun
    
    MaildirStore cochrun-local
    Path ~/Maildir/cochrun/
    Inbox ~/Maildir/cochrun/INBOX
    Subfolders Verbatim
    
    # CONNECTIONS SPECIFY LINKS BETWEEN REMOTE AND LOCAL FOLDERS
    #
    # CONNECTIONS ARE SPECIFIED USING PATTERNS, WHICH MATCH REMOTE MAIl
    # FOLDERS. SOME COMMONLY USED PATTERS INCLUDE:
    #
    # 1 "*" TO MATCH EVERYTHING
    # 2 "!DIR" TO EXCLUDE "DIR"
    # 3 "DIR" TO MATCH DIR
    
    Channel gmail-inbox
    Far :gmail-remote:
    Near :gmail-local:
    Patterns "INBOX"
    Create Both
    Expunge Both
    SyncState *
    
    Channel gmail-trash
    Far :gmail-remote:"[Gmail]/Bin"
    Near :gmail-local:"[Gmail].Bin"
    Create Both
    Expunge Both
    SyncState *
    
    Channel gmail-sent
    Far :gmail-remote:"[Gmail]/Sent Mail"
    Near :gmail-local:"[Gmail].Sent Mail"
    Create Both
    Expunge Both
    SyncState *
    
    Channel gmail-all
    Far :gmail-remote:"[Gmail]/All Mail"
    Near :gmail-local:"[Gmail].All Mail"
    Create Both
    Expunge Both
    SyncState *
    
    Channel gmail-starred
    Far :gmail-remote:"[Gmail]/Starred"
    Near :gmail-local:"[Gmail].Starred"
    Create Both
    Expunge Both
    SyncState *
    
    Channel office-inbox
    Far :office-remote:
    Near :office-local:
    Patterns "*"
    Create Both
    Expunge Both
    SyncState *
    
    Channel outlook-inbox
    Far :outlook-remote:
    Near :outlook-local:
    Patterns "*"
    Create Both
    Expunge Both
    SyncState *
    
    Channel cochrun-inbox
    Far :cochrun-remote:
    Near :cochrun-local:
    Patterns "*"
    Create Both
    Expunge Both
    SyncState *
    # GROUPS PUT TOGETHER CHANNELS, SO THAT WE CAN INVOKE
    # MBSYNC ON A GROUP TO SYNC ALL CHANNELS
    #
    # FOR INSTANCE: "mbsync gmail" GETS MAIL FROM
    # "gmail-inbox", "gmail-sent", and "gmail-trash"
    #
    # Group gmail
    # Channel gmail-inbox
    # Channel gmail-sent
    # Channel gmail-trash
    # Channel gmail-all
    # Channel gmail-starred
    
    Group office
    Channel office-inbox
    
    Group outlook
    Channel outlook-inbox
    
    Group cochrun
    Channel cochrun-inbox
    '';
  };
  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 = if builtins.readFile "/etc/hostname" == "syl\n" then ../mpv-syl else ../mpv;
  };

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

  services.syncthing.enable = true;
  # services.kdeconnect.enable = true;
  services.easyeffects.enable = 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 = ../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 = ../firefox/chrome;
    recursive = true;
  };

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

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

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

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

  programs.nushell = {
    enable = true;
  };

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

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

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

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

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

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

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

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

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

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

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

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

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

  home.file.".config/picom.conf" = {
    source = ../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;
  };

  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-face auto_complete="fg=238"
    # 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"];
      };
    };
  };

}