unit frmOverride;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Layouts, FMX.ListBox, FMX.Controls.Presentation, FMX.StdCtrls,
  FMX.Edit, untCard;

type
  TFormOverride = class(TForm)
    edtLevelOverride: TEdit;
    edtRankOverride: TEdit;
    cmbAttrOverride: TComboBox;
    cmbRaceOverride: TComboBox;
    edtAtkOverride: TEdit;
    edtDefOverride: TEdit;
    btnOK: TCornerButton;
    btnCancel: TCornerButton;
    chkUseLevelOverride: TCheckBox;
    chkUseRankOverride: TCheckBox;
    chkUseAttrOverride: TCheckBox;
    chkUseRaceOverride: TCheckBox;
    chkUseAtkOverride: TCheckBox;
    chkUseDefOverride: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
  private
    FOverrideCard: ICard;
    FOverrideInfo: ICardOverride;
    procedure SetOverrideCard(const Value: ICard);
  public
  published
    property OverrideCard: ICard read FOverrideCard write SetOverrideCard;
    property OverrideInfo: ICardOverride read FOverrideInfo;
  end;

var
  FormOverride: TFormOverride;

implementation

uses
  untAttrRace, untTextItem, frmStyleModule;

{$R *.fmx}

{ TFormOverride }

procedure TFormOverride.btnOKClick(Sender: TObject);
begin
  FOverrideInfo := TCardOverride.Create;

  FOverrideInfo.SetUseLevel(chkUseLevelOverride.IsChecked);
  FOverrideInfo.SetUseRank(chkUseRankOverride.IsChecked);
  FOverrideInfo.SetUseAttr(chkUseAttrOverride.IsChecked);
  FOverrideInfo.SetUseRace(chkUseRaceOverride.IsChecked);
  FOverrideInfo.SetUseAtk(chkUseAtkOverride.IsChecked);
  FOverrideInfo.SetUseDef(chkUseDefOverride.IsChecked);

  FOverrideInfo.SetLevel(StrToIntDef(edtLevelOverride.Text, 0));
  FOverrideInfo.SetRank(StrToIntDef(edtRankOverride.Text, 0));
  if cmbAttrOverride.ItemIndex = 0 then begin
    FOverrideInfo.SetAttr(0);
  end else begin
    FOverrideInfo.SetAttr(TTextItem(cmbAttrOverride.ListItems[cmbAttrOverride.ItemIndex]).OptionId);
  end;
  if cmbRaceOverride.ItemIndex = 0 then begin
    FOverrideInfo.SetRace(0);
  end else begin
    FOverrideInfo.SetRace(TTextItem(cmbRaceOverride.ListItems[cmbRaceOverride.ItemIndex]).OptionId);
  end;
  FOverrideInfo.SetAtk(StrToIntDef(edtAtkOverride.Text, 0));
  FOverrideInfo.SetDef(StrToIntDef(edtDefOverride.Text, 0));
end;

procedure TFormOverride.FormCreate(Sender: TObject);
var
  i: Integer;
  arr: TArray<TTriple<Int64, String, String>>;
  item: TTriple<Int64, String, String>;
begin
  cmbAttrOverride.Clear;
  cmbRaceOverride.Clear;

  cmbAttrOverride.AddObject(TTextItem.Create(cmbAttrOverride, '无', 0));
  cmbRaceOverride.AddObject(TTextItem.Create(cmbRaceOverride, '无', 0));

  arr := TYGOValueList.GetAttrList;
  for i := 0 to Length(arr) - 1 do begin
    item := arr[i];
    cmbAttrOverride.AddObject(TTextItem.Create(cmbAttrOverride, item.Third, item.First));
  end;

  arr := TYGOValueList.GetRaceList;
  for i := 0 to Length(arr) - 1 do begin
    item := arr[i];
    cmbRaceOverride.AddObject(TTextItem.Create(cmbRaceOverride, item.Third  + '族', item.First));
  end;

  cmbAttrOverride.ItemIndex := 0;
  cmbRaceOverride.ItemIndex := 0;
end;

procedure TFormOverride.SetOverrideCard(const Value: ICard);
var
  item: TTextItem;
  i: Integer;
begin
  FOverrideCard := Value;

  // value input
  chkUseLevelOverride.IsChecked := FOverrideCard.GetOverride.GetUseLevel;
  chkUseRankOverride.IsChecked := FOverrideCard.GetOverride.GetUseRank;
  chkUseAttrOverride.IsChecked := FOverrideCard.GetOverride.GetUseAttr;
  chkUseRaceOverride.IsChecked := FOverrideCard.GetOverride.GetUseRace;
  chkUseAtkOverride.IsChecked := FOverrideCard.GetOverride.GetUseAtk;
  chkUseDefOverride.IsChecked := FOverrideCard.GetOverride.GetUseDef;

  edtLevelOverride.Text := IntToStr(FOverrideCard.GetOverride.GetLevel);
  edtRankOverride.Text := IntToStr(FOverrideCard.GetOverride.GetRank);
  edtAtkOverride.Text := IntToStr(FOverrideCard.GetOverride.GetAtk);
  edtDefOverride.Text := IntToStr(FOverrideCard.GetOverride.GetDef);

  if FOverrideCard.GetOverride.GetAttr = 0 then begin
    cmbAttrOverride.ItemIndex := 0;
  end else begin
    for i := 0 to cmbAttrOverride.Count - 1 do begin
      item := TTextItem(cmbAttrOverride.ListItems[i]);
      if item.OptionId = FOverrideCard.GetOverride.GetAttr then begin
        cmbAttrOverride.ItemIndex := i;
        Break;
      end;
    end;
  end;

  if FOverrideCard.GetOverride.GetRace = 0 then begin
    cmbRaceOverride.ItemIndex := 0;
  end else begin
    for i := 0 to cmbRaceOverride.Count - 1  do begin
      item := TTextItem(cmbRaceOverride.ListItems[i]);
      if item.OptionId = FOverrideCard.GetOverride.GetRace then begin
        cmbRaceOverride.ItemIndex := i;
        Break;
      end;
    end;
  end;


  // enabled
  chkUseLevelOverride.Enabled := (not FOverrideCard.GetIsXyzMonster) and (not FOverrideCard.GetIsLinkMonster);
  edtLevelOverride.Enabled := chkUseLevelOverride.Enabled;

  chkUseRankOverride.Enabled := FOverrideCard.GetIsXyzMonster;
  edtRankOverride.Enabled := chkUseRankOverride.Enabled;

end;

end.
