{ config, lib, inputs, pkgs, self, ... }:
let
  cfg = config.profiles.base;

  neotheme = config.neotheme;
in
{
  imports = [
    ../../../modules/nixpkgs.nix
    ../../modules/programs/zshell
    #../../modules/programs/zellij
  ];

  options.profiles.base.enable = lib.mkEnableOption
    "The base profile, contains general configurations.";

  config = lib.mkIf cfg.enable {
    home.sessionVariables = rec {
      EDITOR = "nvim";
      VISUAL = EDITOR;
      GIT_EDITOR = EDITOR;

      # Airport
      AIRPORT_USER = "20963";
      AIRPORT_HOST = "hk-s020.rsync.net";

      # TODO: https://github.com/nix-community/home-manager/pull/5901
      SSH_AUTH_SOCK = lib.mkIf (config.services.gpg-agent.enable && pkgs.stdenv.isDarwin) "$(${config.programs.gpg.package}/bin/gpgconf --list-dirs agent-ssh-socket)";
    };

    home.keyboard.layout = "us";
    home.packages = [
      # Nix Tools
      pkgs.nh
      pkgs.nix-index
      pkgs.nix-tree
      pkgs.nix-update
      pkgs.hydra-check

      pkgs.age
      pkgs.aria2
      pkgs.bottom
      pkgs.calc
      pkgs.croc
      pkgs.comma
      pkgs.fd

      pkgs.fzf
      pkgs.libnotify
      pkgs.ouch
      pkgs.procs
      pkgs.sops
      pkgs.tree
      pkgs.units
      pkgs.neovim

      # Common build tools
      (pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override { extensions = [ "rust-src" ]; }))
      pkgs.go
      pkgs.gcc

      (pkgs.writeShellScriptBin "0x0" (import ./scripts/0x0.nix))
      (pkgs.writeShellScriptBin "airport" (import ./scripts/airport.nix))
    ];


    programs = {
      bat.enable = true;
      direnv.enable = true;
      direnv.nix-direnv.enable = true;
      eza.enable = true;
      eza.enableZshIntegration = true;
      gpg.enable = true;
      gpg.settings.keyserver = "keys.openpgp.org";
      home-manager.enable = true;

      htop = {
        enable = true;

        settings = {
          color_scheme = 6;
          # 0 = false, 1 = true
          cpu_count_from_one = 0;
          fields = with config.lib.htop.fields; [
            PID
            USER
            PRIORITY
            NICE
            M_SIZE
            M_RESIDENT
            M_SHARE
            STATE
            PERCENT_CPU
            PERCENT_MEM
            TIME
            COMM
          ];
          highlight_base_name = 1;
          highlight_megabytes = 1;
          highlight_threads = 1;
        } // (with config.lib.htop; leftMeters [
          (bar "LeftCPUs4")
          (bar "Memory")
          (bar "Swap")
          (text "NetworkIO")
          (text "DiskIO")
        ]) // (with config.lib.htop; rightMeters [
          (bar "RightCPUs4")
          (text "Tasks")
          (text "LoadAverage")
          (text "Uptime")
          (text "Systemd")
        ]);
      };

      tmux = {
        enable = true;

        prefix = "C-f";
        terminal = "xterm-256color";
        shell = "${pkgs.zsh}/bin/zsh";
        baseIndex = 1;
        keyMode = "vi";

        escapeTime = 0;

        extraConfig = import ./config/tmux.nix;
      };



      zsh = {
        enable = true;

        enableCompletion = true;
        autosuggestion.enable = true;

        autocd = true;

        dotDir = ".config/zsh";

        plugins = [
          {
            name = "powerlevel10k";
            src = pkgs.zsh-powerlevel10k;
            file = "share/zsh-powerlevel10k/powerlevel10k.zsh-theme";
          }

          {
            name = "powerlevel10k-config";
            src = lib.cleanSource ./p10k-config;
            file = "p10k.zsh";
          }

          {
            name = "zsh-syntax-highlighting";
            src = pkgs.fetchFromGitHub {
              owner = "zsh-users";
              repo = "zsh-syntax-highlighting";
              rev = "0.7.1";
              sha256 = "03r6hpb5fy4yaakqm3lbf4xcvd408r44jgpv4lnzl9asp4sb9qc0";
            };
          }

          (lib.mkIf pkgs.stdenv.isLinux {
            name = "doas-zsh-plugin";
            src = pkgs.fetchFromGitHub {
              owner = "anatolykopyl";
              repo = "doas-zsh-plugin";
              rev = "17d0b55ca2acd12f7acc9e38c4ecaf413725be18";
              sha256 = "sha256-10rcF9cho9GuZCFQVIdFjvHCAlTLHNaLY4twxjT2jcE=";
            };
            file = "doas.plugin.zsh";
          })

          (lib.mkIf pkgs.stdenv.isDarwin {
            name = "sudo-zsh-plugin";
            src = pkgs.oh-my-zsh.src;
            file = "plugins/sudo/sudo.plugin.zsh";
          })

          {
            name = "zsh-vi-mode";
            src = pkgs.zsh-vi-mode;
            file = "share/zsh-vi-mode/zsh-vi-mode.plugin.zsh";
          }
        ];

        shellAliases = config.programs.zshell.aliases;
      };

      zshell.aliases = {
        vim = "nvim";
        nvimdiff = "nvim -d";
        pardl = "aria2c -c -s 16 -x 16 -k 1M -j 1";
      };

      zoxide = {
        enable = true;
        enableZshIntegration = true;
      };

      zellij = {
        enable = true;

        settings = {
          default_mode = "normal";
          pane_frames = false;
          simplified_ui = true; # bitmap fonts

          themes = {
            default = {
              # Industrial Jellybeans theme
              bg = [ 21 21 21 ];
              fg = [ 136 136 136 ];
              black = [ 28 28 28 ];
              gray = [ 136 136 136 ];
              white = [ 232 232 211 ];
              red = [ 141 61 38 ];
              orange = [ 250 187 110 ];
              yellow = [ 250 208 122 ];
              green = [ 153 173 106 ];
              blue = [ 129 151 191 ];
              cyan = [ 143 191 220 ];
              magenta = [ 198 182 238 ];
            };
          };
        };
      };
    };

    xdg.configFile."zellij/layouts/sdev.kdl".text = ''
      layout {
          pane size=1 borderless=true {
              plugin location="zellij:tab-bar"
          }
          pane split_direction="vertical" {
              pane size="80%" command="nvim"
              pane size="20%"
          }
          pane size=2 borderless=true {
              plugin location="zellij:status-bar"
          }
      }
    '';

    xdg.configFile."zellij/layouts/hdev.kdl".text = ''
      layout {
          pane size=1 borderless=true {
              plugin location="zellij:tab-bar"
          }
          pane split_direction="horizontal" {
              pane size="80%" command="nvim"
              pane size="20%"
          }
          pane size=2 borderless=true {
              plugin location="zellij:status-bar"
          }
      }
    '';

    services = {
      gpg-agent = {
        enable = true;
        pinentryPackage = lib.mkIf pkgs.stdenv.isLinux pkgs.pinentry-gnome3;
        defaultCacheTtl = 3600;
        defaultCacheTtlSsh = 3600;
        maxCacheTtl = 86400;
        maxCacheTtlSsh = 86400;
        enableSshSupport = true;
        sshKeys = [ "3C79004101E524D10B94F9EE9D3744F4B25F8795" "159371696FD285FF0811692FBFAA6E4AF017CDDE" ];
        extraConfig = ''
          allow-preset-passphrase
        '';
      };
    };
  };
}