use async_trait::async_trait;
use chrono::Utc;
use k8s_openapi::api::rbac::v1::{ClusterRole, ClusterRoleBinding, Role, RoleBinding};
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 KubeRole {
  pub namespace: String,
  pub name: String,
  pub age: String,
  k8s_obj: Role,
}

#[derive(Clone, Debug, PartialEq)]
pub struct KubeRoleBinding {
  pub namespace: String,
  pub name: String,
  pub role: String,
  pub age: String,
  k8s_obj: RoleBinding,
}

#[derive(Clone, Debug, PartialEq)]
pub struct KubeClusterRole {
  pub name: String,
  pub age: String,
  k8s_obj: ClusterRole,
}

#[derive(Clone, Debug, PartialEq)]
pub struct KubeClusterRoleBinding {
  pub name: String,
  pub role: String,
  pub age: String,
  k8s_obj: ClusterRoleBinding,
}

impl From<Role> for KubeRole {
  fn from(role: Role) -> Self {
    KubeRole {
      namespace: role.metadata.namespace.clone().unwrap_or_default(),
      name: role.metadata.name.clone().unwrap_or_default(),
      age: utils::to_age(role.metadata.creation_timestamp.as_ref(), Utc::now()),
      k8s_obj: utils::sanitize_obj(role),
    }
  }
}

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

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

static ROLES_TITLE: &str = "Roles";

pub struct RoleResource {}

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

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

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

const ROLE_COLUMNS: [ColumnDef; 3] = [
  ColumnDef::all("Namespace", 40, 40, 40),
  ColumnDef::all("Name", 40, 40, 40),
  ColumnDef::all("Age", 20, 20, 20),
];

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

  let (headers, widths) = responsive_columns(&ROLE_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.roles,
      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.age.to_owned()),
      ])
      .style(style_text(app.palette))
    },
    app.palette,
    is_loading,
  );
}

impl From<ClusterRole> for KubeClusterRole {
  fn from(cluster_role: ClusterRole) -> Self {
    KubeClusterRole {
      name: cluster_role.metadata.name.clone().unwrap_or_default(),
      age: utils::to_age(
        cluster_role.metadata.creation_timestamp.as_ref(),
        Utc::now(),
      ),
      k8s_obj: utils::sanitize_obj(cluster_role),
    }
  }
}

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

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

static CLUSTER_ROLES_TITLE: &str = "ClusterRoles";

pub struct ClusterRoleResource {}

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

  async fn get_resource(nw: &Network<'_>) {
    let items: Vec<KubeClusterRole> = nw.get_resources(ClusterRole::into).await;

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

const CR_COLUMNS: [ColumnDef; 2] = [
  ColumnDef::all("Name", 50, 50, 50),
  ColumnDef::all("Age", 50, 50, 50),
];

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

  let (headers, widths) = responsive_columns(&CR_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.cluster_roles,
      table_headers: headers,
      column_widths: widths,
    },
    |c| {
      Row::new(vec![
        Cell::from(c.name.to_owned()),
        Cell::from(c.age.to_owned()),
      ])
      .style(style_text(app.palette))
    },
    app.palette,
    is_loading,
  );
}

impl From<RoleBinding> for KubeRoleBinding {
  fn from(role_binding: RoleBinding) -> Self {
    KubeRoleBinding {
      namespace: role_binding.metadata.namespace.clone().unwrap_or_default(),
      name: role_binding.metadata.name.clone().unwrap_or_default(),
      role: role_binding.role_ref.name.clone(),
      age: utils::to_age(
        role_binding.metadata.creation_timestamp.as_ref(),
        Utc::now(),
      ),
      k8s_obj: utils::sanitize_obj(role_binding),
    }
  }
}
impl Named for KubeRoleBinding {
  fn get_name(&self) -> &String {
    &self.name
  }
}

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

static ROLE_BINDINGS_TITLE: &str = "RoleBindings";

pub struct RoleBindingResource {}

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

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

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

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

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

  let (headers, widths) = responsive_columns(&RB_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.role_bindings,
      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.role.to_owned()),
        Cell::from(c.age.to_owned()),
      ])
      .style(style_text(app.palette))
    },
    app.palette,
    is_loading,
  );
}

impl From<ClusterRoleBinding> for KubeClusterRoleBinding {
  fn from(crb: ClusterRoleBinding) -> Self {
    KubeClusterRoleBinding {
      name: crb.metadata.name.clone().unwrap_or_default(),
      role: format!("{}/{}", crb.role_ref.kind, crb.role_ref.name),
      age: utils::to_age(crb.metadata.creation_timestamp.as_ref(), Utc::now()),
      k8s_obj: utils::sanitize_obj(crb),
    }
  }
}

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

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

static CLUSTER_ROLES_BINDING_TITLE: &str = "ClusterRoleBinding";

pub struct ClusterRoleBindingResource {}

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

  async fn get_resource(nw: &Network<'_>) {
    let items: Vec<KubeClusterRoleBinding> = nw.get_resources(ClusterRoleBinding::into).await;

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

const CRB_COLUMNS: [ColumnDef; 3] = [
  ColumnDef::all("Name", 40, 40, 40),
  ColumnDef::all("Role", 40, 40, 40),
  ColumnDef::all("Age", 20, 20, 20),
];

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

  let (headers, widths) = responsive_columns(&CRB_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.cluster_role_bindings,
      table_headers: headers,
      column_widths: widths,
    },
    |c| {
      Row::new(vec![
        Cell::from(c.name.to_owned()),
        Cell::from(c.role.to_owned()),
        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::{
    roles::{KubeClusterRole, KubeClusterRoleBinding, KubeRole, KubeRoleBinding},
    test_utils::{convert_resource_from_file, get_time},
    utils,
  };

  #[test]
  fn test_roles_binding_from_rbac_api() {
    let (roles, roles_list): (Vec<KubeRole>, Vec<_>) = convert_resource_from_file("roles");

    assert_eq!(roles.len(), 1);
    assert_eq!(
      roles[0],
      KubeRole {
        namespace: "default".to_string(),
        name: "kiali-viewer".into(),
        age: utils::to_age(Some(&get_time("2022-06-27T16:33:06Z")), Utc::now()),
        k8s_obj: roles_list[0].clone(),
      }
    )
  }

  #[test]
  fn test_cluster_roles_from_rbac_api() {
    let (cluster_roles, cluster_roles_list): (Vec<KubeClusterRole>, Vec<_>) =
      convert_resource_from_file("clusterroles");

    assert_eq!(cluster_roles.len(), 1);
    assert_eq!(
      cluster_roles[0],
      KubeClusterRole {
        name: "admin".into(),
        age: utils::to_age(Some(&get_time("2021-12-14T11:04:22Z")), Utc::now()),
        k8s_obj: cluster_roles_list[0].clone(),
      }
    )
  }

  #[test]
  fn test_role_binding_from_rbac_api() {
    let (role_bindings, rolebindings_list): (Vec<KubeRoleBinding>, Vec<_>) =
      convert_resource_from_file("role_bindings");

    assert_eq!(role_bindings.len(), 1);
    assert_eq!(
      role_bindings[0],
      KubeRoleBinding {
        namespace: "default".to_string(),
        name: "kiali".into(),
        role: "kiali-viewer".into(),
        age: utils::to_age(Some(&get_time("2022-06-27T16:33:07Z")), Utc::now()),
        k8s_obj: rolebindings_list[0].clone(),
      }
    )
  }

  #[test]
  fn test_cluster_role_bindings_from_rbac_api() {
    let (cluster_role_binding, cluster_role_bindings_list): (Vec<KubeClusterRoleBinding>, Vec<_>) =
      convert_resource_from_file("clusterrole_binding");

    assert_eq!(cluster_role_binding.len(), 2);
    assert_eq!(
      cluster_role_binding[0],
      KubeClusterRoleBinding {
        name: "admin-user".into(),
        role: "ClusterRole/cluster-admin".into(),
        age: utils::to_age(Some(&get_time("2022-03-02T16:50:53Z")), Utc::now()),
        k8s_obj: cluster_role_bindings_list[0].clone(),
      }
    )
  }
}
