use async_trait::async_trait;
use chrono::Utc;
use k8s_openapi::api::core::v1::ServiceAccount;
use ratatui::{
  layout::Rect,
  widgets::{Cell, Row},
  Frame,
};

use super::{
  models::{AppResource, KubeResource, Named},
  utils::{self},
  ActiveBlock, App,
};
use crate::{
  draw_resource_tab,
  network::Network,
  ui::utils::{
    describe_yaml_and_esc_hint, draw_describe_block, draw_resource_block, draw_yaml_block,
    get_describe_active, get_resource_title, help_bold_line, responsive_columns, style_text,
    title_with_dual_style, ColumnDef, ResourceTableProps, ViewTier,
  },
};

#[derive(Clone, Debug, PartialEq)]
pub struct KubeSvcAcct {
  pub namespace: String,
  pub name: String,
  pub secrets: i32,
  pub age: String,
  k8s_obj: ServiceAccount,
}

// Get length of a vector
impl From<ServiceAccount> for KubeSvcAcct {
  fn from(acct: ServiceAccount) -> Self {
    KubeSvcAcct {
      namespace: acct.metadata.namespace.clone().unwrap_or_default(),
      name: acct.metadata.name.clone().unwrap_or_default(),
      secrets: acct.secrets.clone().unwrap_or_default().len() as i32,
      age: utils::to_age(acct.metadata.creation_timestamp.as_ref(), Utc::now()),
      k8s_obj: utils::sanitize_obj(acct),
    }
  }
}

impl Named for KubeSvcAcct {
  fn get_name(&self) -> &String {
    &self.name
  }
}

impl KubeResource<ServiceAccount> for KubeSvcAcct {
  fn get_k8s_obj(&self) -> &ServiceAccount {
    &self.k8s_obj
  }
}

static SVC_ACCT_TITLE: &str = "ServiceAccounts";

pub struct SvcAcctResource {}

#[async_trait]
impl AppResource for SvcAcctResource {
  fn render(block: ActiveBlock, f: &mut Frame<'_>, app: &mut App, area: Rect) {
    draw_resource_tab!(
      SVC_ACCT_TITLE,
      block,
      f,
      app,
      area,
      Self::render,
      draw_block,
      app.data.service_accounts
    );
  }

  async fn get_resource(nw: &Network<'_>) {
    let items: Vec<KubeSvcAcct> = nw.get_namespaced_resources(ServiceAccount::into).await;

    let mut app = nw.app.lock().await;
    app.data.service_accounts.set_items(items);
  }
}

const SA_COLUMNS: [ColumnDef; 4] = [
  ColumnDef::all("Namespace", 30, 30, 30),
  ColumnDef::all("Name", 30, 30, 30),
  ColumnDef::all("Secrets", 20, 20, 20),
  ColumnDef::all("Age", 20, 20, 20),
];

fn draw_block(f: &mut Frame<'_>, app: &mut App, area: Rect) {
  let is_loading = app.is_loading();
  let title = get_resource_title(
    app,
    SVC_ACCT_TITLE,
    "",
    app.data.service_accounts.items.len(),
  );

  let (headers, widths) = responsive_columns(&SA_COLUMNS, ViewTier::Compact);

  draw_resource_block(
    f,
    area,
    ResourceTableProps {
      title,
      inline_help: help_bold_line(describe_yaml_and_esc_hint(), app.palette),
      resource: &mut app.data.service_accounts,
      table_headers: headers,
      column_widths: widths,
    },
    |c| {
      Row::new(vec![
        Cell::from(c.namespace.to_owned()),
        Cell::from(c.name.to_owned()),
        Cell::from(c.secrets.to_string()),
        Cell::from(c.age.to_owned()),
      ])
      .style(style_text(app.palette))
    },
    app.palette,
    is_loading,
  );
}

#[cfg(test)]
mod tests {
  use chrono::Utc;

  use crate::app::{
    serviceaccounts::KubeSvcAcct,
    test_utils::{convert_resource_from_file, get_time},
    utils,
  };

  #[test]
  fn test_service_accounts_from_api() {
    let (serviceaccounts, serviceaccounts_list): (Vec<KubeSvcAcct>, Vec<_>) =
      convert_resource_from_file("serviceaccounts");

    assert_eq!(serviceaccounts.len(), 43);
    assert_eq!(
      serviceaccounts[0],
      KubeSvcAcct {
        namespace: "kube-node-lease".to_string(),
        name: "default".into(),
        secrets: 3,
        age: utils::to_age(Some(&get_time("2023-06-30T17:13:19Z")), Utc::now()),
        k8s_obj: serviceaccounts_list[0].clone(),
      }
    )
  }
}
