local cm,m = GetID()
cm.name = "和弦之七音服"
cm.group = { }
function cm.initial_effect(c)
    aux.AddLinkProcedure(c,nil,2,nil,function(g,lc) return g:IsExists(Card.IsLinkSetCard,1,nil,0x162) end)
	c:EnableReviveLimit()
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(m,0))
	e1:SetCategory(CATEGORY_TOEXTRA+CATEGORY_TOHAND+CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetProperty(EFFECT_FLAG_DELAY)
	e1:SetCountLimit(1,m)
	e1:SetCondition(cm.con)
	e1:SetTarget(cm.tg)
	e1:SetOperation(cm.op)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e2:SetTargetRange(0,1)
	e2:SetTarget(cm.limit)
	c:RegisterEffect(e2)
	local e3=e2:Clone()
	e3:SetCode(EFFECT_CANNOT_ACTIVATE)
	e3:SetRange(LOCATION_MZONE)
	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e3:SetValue(cm.limit2)
	c:RegisterEffect(e3)
	local e4=Effect.CreateEffect(c)
	e4:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_TOGRAVE)
	e4:SetType(EFFECT_TYPE_IGNITION)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCountLimit(1,m+100)
	e4:SetCondition(function(e,tp,eg,ep,ev,re,r,rp) return Duel.GetMatchingGroup(cm.ff,tp,LOCATION_MZONE+LOCATION_EXTRA,0,1,nil):GetClassCount(Card.GetCode)>=7 end)
	e4:SetTarget(cm.tg2)
	e4:SetOperation(cm.op2)
	c:RegisterEffect(e4)
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK) and Duel.IsExistingMatchingCard(function(c) return c:IsFaceup() and c:IsSetCard(0x162) end,tp,LOCATION_EXTRA,0,2,nil)
end
function cm.f(c)
	return c:IsSetCard(0x162) and (c:IsAbleToHand() or c:IsAbleToExtra())
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(cm.f,tp,LOCATION_DECK,0,1,nil) end
end
function cm.op(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_OPERATECARD)
	local g=Duel.SelectMatchingCard(tp,cm.f,tp,LOCATION_DECK,0,1,1,nil)
	if g:GetCount()<=0 then return end
	local tc=g:GetFirst()
	if tc:IsAbleToHand() and (not tc:IsAbleToExtra() or Duel.SelectOption(tp,1190,1193)==0) then
		Duel.SendtoHand(tc,nil,REASON_EFFECT)
		Duel.ConfirmCards(1-tp,tc)
	else
		Duel.SendtoExtraP(tc,nil,REASON_EFFECT)
	end
end
function cm.ff(c) 
    return (c:IsFaceup() or c:IsLocation(LOCATION_MZONE)) and c:IsSetCard(0x162) 
end
function cm.limit(e,c,sump,sumtype,sumpos,targetp)
    local g = Duel.GetMatchingGroup(cm.ff,tp,LOCATION_MZONE+LOCATION_EXTRA,0,1,nil)
	local lv = c:GetLevel()
	if lv==0 then return false end
	for vc in aux.Next(g) do
        local lvc = vc:GetLevel()
        if lv == lvc then return true end
    end
end
function cm.ck(t,b)
    for k,c in ipairs(t) do
        if c==b then return false end
    end
    return true
end
function cm.limit2(e,te,tp)
    local p = e:GetHandlerPlayer()
    local g = Duel.GetMatchingGroup(cm.ff,p,LOCATION_MZONE+LOCATION_EXTRA,0,1,nil)
    if not te:IsActiveType(TYPE_MONSTER) then return false end
    local lv = te:GetHandler():GetLevel()
	if lv==0 then return false end
	for vc in aux.Next(g) do
        local lvc = vc:GetLevel()
        if lv == lvc then return true end
    end
end
function cm.tg2(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and Duel.IsExistingMatchingCard(function(c,e,tp) return c:IsCanBeSpecialSummoned(e,0,tp,true,false) end,tp,LOCATION_EXTRA,0,1,nil,e,tp) end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_EXTRA)
end
function cm.op2(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	local g=Duel.SelectMatchingCard(tp,function(c,e,tp) return c:IsCanBeSpecialSummoned(e,0,tp,true,false) end,tp,LOCATION_EXTRA,0,1,1,nil,e,tp)
	Duel.SpecialSummon(g,0,tp,tp,true,false,POS_FACEUP)
end