using Plots, ComplexPhasePortrait, ApproxFun, SingularIntegralEquations
gr();

μ = z -> -im*(sqrt(z-1)sqrt(z+1) - z)
Φ = (θ,z) -> exp(-im*θ)*z + sin(θ)μ(z)
u = (θ,x,y) -> imag(Φ(θ, x + im*y))

xx = yy = range(-3; stop=3 , length=500)
contour(xx, yy, u.(1.3,xx',yy); nlevels = 100)
plot!(Segment(-1.,1.); color=:black, label="obstacle")

D = randn()
θ = 0.1
x = Fun()
w = 2sin(θ) * (D-x^2)/sqrt(1-x^2)
hilbert(w,0.2)- (-2sin(θ)*0.2)

a = 0.3
θ = 1.3
Γ = Segment(-1,-a) ∪ Segment(a, 1)

x = Fun(Γ)
sp = PiecewiseSpace(JacobiWeight.(0.5,0.5,components(Γ))...)
H = Hilbert(sp)


o₁ = Fun(x -> -1 ≤ x ≤ -a ? 1 : 0, Γ )
o₂ = Fun(x -> a ≤ x ≤ 1 ? 1 : 0, Γ )

a, b, f = [o₁ o₂ H] \ [-2x*sin(θ)]


Φ = (θ,z) -> exp(-im*θ)*z + cauchy(f, z)
v = (θ,x,y) -> imag(Φ(θ, x + im*y))

xx = yy = range(-3.; stop=3., length=500)
contour(xx, yy, v.(θ, xx',yy); nlevels = 100)
plot!(Γ; color=:black, label="obstacle")