import Mathlib
import Development.AbelianVarietyTheory.Definitions.LatticeFrames.ToOneSucc
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.IsOpenKerOfContinuous
open MeasureTheory ValuativeRel Matrix GlobalTheory.SplitWeil GlobalTheory.RootEx GlobalTheory.StepData GlobalTheory.PosOne
open GlobalTheory.IndefiniteRowSideFullHecke.FourierJacobiAverage (app HasTensorVal fl opEFJ IsQuotIntFJ IsFJ Represents)
open GlobalTheory.IndefiniteRowSideFullHecke.FramePositiveSum (IsCO rescale)
open Filter Topology

noncomputable section
namespace GlobalTheory.PosOne.UniformComb
open GlobalTheory.IndefiniteRowSideFullHecke.PositiveTwistAverage GlobalTheory.RootEx.UniformLattices

theorem comb_sum {G ι : Type} (T : Finset ι) (g : ι → G) (a : ℂ) (f : G → ℂ) :
    comb (∑ t ∈ T, Finsupp.single (g t) a) f = a * ∑ t ∈ T, f (g t) := by
  unfold comb
  rw [← Finsupp.sum_finset_sum_index (fun _ => zero_mul _) (fun _ _ _ => add_mul _ _ _), Finset.mul_sum]
  refine Finset.sum_congr rfl fun t _ => ?_
  rw [Finsupp.sum_single_index (zero_mul _)]

variable {k : Type} [Field k] [ValuativeRel k] [TopologicalSpace k] [IsNonarchimedeanLocalField k]
  [MeasurableSpace k] [BorelSpace k] {n : Type} [Fintype n] [DecidableEq n] {c : ℕ}

theorem exists_comb {ψ : AddChar k Circle} (hψ : Continuous ψ) (hψ1 : ψ ≠ 1) (ν : Measure k) [ν.IsAddHaarMeasure]
    (g : (n → k) → GL (n ⊕ Fin c) k) (hg : Continuous g) (hg1 : g 0 = 1)
    (hadd : ∀ a b, g (a + b) = g a * g b) (U : Subgroup (GL (n ⊕ Fin c) k)) (hU : IsOpen (U : Set (GL (n ⊕ Fin c) k)))
    (W : Submodule 𝒪[k] (n → k)) (hW : W.IsLattice k) :
    ∃ e : GL (n ⊕ Fin c) k →₀ ℂ, ∀ f : GL (n ⊕ Fin c) k → ℂ,
      (∀ x b, g b ∈ U → f (g x * g b) = f (g x)) → (∀ x, x ∉ perp ψ W → f (g x) = 0) →
      ∫ x, f (g x) ∂(Measure.pi fun _ => ν) = comb e f := by
  have := secondCountable k
  have := t2Space k
  have hV : {b : n → k | g b ∈ U} ∈ 𝓝 (0 : n → k) :=
    hg.continuousAt.preimage_mem_nhds (hU.mem_nhds (by simp [hg1, U.one_mem]))
  obtain ⟨t, ht⟩ := exists_ballLat_subset (inter_mem hV (perp_mem_nhds hψ W hW))
  have hBl := ballLat_isLattice (m := n) t
  obtain ⟨T, hT⟩ := exists_isReps (perp ψ W) (ballLat n t).toAddSubgroup (isCompact_perp hψ hψ1 W hW)
    (LatPair.isClopen_lattice (ballLat n t)).2
  refine ⟨∑ x ∈ T, Finsupp.single (g x)
    ((((Measure.pi fun _ : n => ν).real ((ballLat n t).toAddSubgroup : Set (n → k)) : ℝ) : ℂ)), fun f h1 h0 => ?_⟩
  rw [comb_sum]
  refine integral_eq_sum_reps (Measure.pi fun _ : n => ν) (fun x hx => (ht hx).2)
    (LatPair.isClopen_lattice (ballLat n t)).2 (isCompact_lattice _ hBl) hT (fun x => f (g x)) h0
    fun x b hb => ?_
  simp only [hadd]
  exact h1 x b (ht hb).1

end GlobalTheory.PosOne.UniformComb

namespace GlobalTheory.PosOne
open GlobalTheory.IndefiniteRowSideFullHecke.PositiveTwistAverage GlobalTheory.RootEx.UniformLattices GlobalTheory.PosOne.UniformComb

theorem exchange_eq_comb : ∀ (k : Type) [Field k] [ValuativeRel k] [TopologicalSpace k] [IsNonarchimedeanLocalField k] [MeasurableSpace k] [BorelSpace k] (n : Type) [Fintype n] [DecidableEq n] (c p : ℕ) (_ : 1 ≤ p) (_ : p < c) (ψ : AddChar k Circle) (_ : Continuous ψ) (_ : ψ ≠ 1) (ν : Measure k) [ν.IsAddHaarMeasure] (U : Subgroup (GL (n ⊕ Fin c) k)) (_ : IsOpen (U : Set (GL (n ⊕ Fin c) k))), ∃ e e' : GL (n ⊕ Fin c) k →₀ ℂ, ∀ (A : Type) [AddCommGroup A] [Module ℂ A] (σ : Representation ℂ (GL (n ⊕ Fin c) k) A) (_ : IsSmoothRep σ) (l : A →ₗ[ℂ] ℂ) (v : A), (∀ u ∈ U, σ u v = v) → (IsNEquiv ψ (p + 1) σ l → exDown ν p σ l v = comb e fun g => l (σ g v)) ∧ (IsNEquiv ψ p σ l → exUp ν p σ l v = comb e' fun g => l (σ g v)) := by
  intro k _ _ _ _ _ _ n _ _ c p hp hc ψ hψ hψ1 ν _ U hU
  have hrowN : {u : n → k | rowE c p u ∈ U} ∈ 𝓝 (0 : n → k) :=
    ExchangeSupport.continuous_rowE.continuousAt.preimage_mem_nhds
      (hU.mem_nhds (by simp [ExchangeSupport.rowE_zero, U.one_mem]))
  have hcolN : {β : n → k | colE c (p + 1) β ∈ U} ∈ 𝓝 (0 : n → k) :=
    ExchangeSupport.continuous_colE.continuousAt.preimage_mem_nhds
      (hU.mem_nhds (by simp [ExchangeSupport.colE_zero, U.one_mem]))
  obtain ⟨s, hs⟩ := exists_ballLat_subset hrowN
  obtain ⟨s', hs'⟩ := exists_ballLat_subset hcolN
  obtain ⟨e, he⟩ := exists_comb hψ hψ1 ν (colE c (p + 1)) ExchangeSupport.continuous_colE
    ExchangeSupport.colE_zero ExchangeSupport.colE_add U hU (ballLat n s) (ballLat_isLattice s)
  obtain ⟨e', he'⟩ := exists_comb hψ hψ1 ν (rowE c p) ExchangeSupport.continuous_rowE
    ExchangeSupport.rowE_zero ExchangeSupport.rowE_add U hU (ballLat n s') (ballLat_isLattice s')
  refine ⟨e, e', fun A _ _ σ hσ l v hv => ⟨fun hl => ?_, fun hl => ?_⟩⟩
  · refine he (fun g => l (σ g v)) (fun x b hb => ?_) (fun x hx => ?_)
    · simp only [map_mul, Module.End.mul_apply, hv _ hb]
    · by_contra hne
      refine hx fun u hu => ?_
      have := ((ExchangeSupport.exchange_support k n c p hp hc ψ hψ hψ1 ν A σ hσ l v).2 hl).2 u x
      rw [hv _ (hs hu)] at this
      have h4 : (ψ (-(u ⬝ᵥ x)) : ℂ) = 1 := mul_right_cancel₀ hne (by rw [one_mul]; exact this.symm)
      rw [← inv_eq_one, ← AddChar.map_neg_eq_inv]
      exact Circle.ext (by simpa only [Circle.coe_one] using h4)
  · refine he' (fun g => l (σ g v)) (fun x b hb => ?_) (fun x hx => ?_)
    · simp only [map_mul, Module.End.mul_apply, hv _ hb]
    · by_contra hne
      refine hx fun β hβ => ?_
      have := ((ExchangeSupport.exchange_support k n c p hp hc ψ hψ hψ1 ν A σ hσ l v).1 hl).2.1 x β
      rw [hv _ (hs' hβ)] at this
      have h4 : (ψ (x ⬝ᵥ β) : ℂ) = 1 := mul_right_cancel₀ hne (by rw [one_mul]; exact this.symm)
      rw [dotProduct_comm]
      exact Circle.ext (by simpa only [Circle.coe_one] using h4)

end GlobalTheory.PosOne
