//
//  MyInfoCardView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import SwiftUI
import Kingfisher

struct MyInfoCardView: View {
    
    let data: MyPageResponse
    let refresh: () -> Void
    
    var body: some View {
        HStack(spacing: 13.3) {
            KFImage(URL(string: data.profileUrl))
                .cancelOnDisappear(true)
                .downsampling(
                    size: CGSize(
                        width: 90,
                        height: 90
                    )
                )
                .resizable()
                .scaledToFill()
                .frame(width: 90, height: 90, alignment: .top)
                .clipShape(Circle())
            
            VStack(alignment: .leading, spacing: 13.3) {
                HStack(spacing: 0) {
                    Text(data.nickname)
                        .font(.custom(Font.bold.rawValue, size: 20))
                        .foregroundColor(Color(hex: "eeeeee"))
                    
                    Spacer()
                    
                    Button(action: {
                        if AppState.shared.roomId <= 0 {
                            AppState.shared.setAppStep(step: .profileUpdate(refresh: refresh))
                        }
                    }) {
                        Image("ic_myinfo_edit")
                            .resizable()
                            .frame(width: 20, height: 20)
                    }
                }
                
                HStack(spacing: 10) {
                    Spacer()
                    
                    if let websiteUrl = data.websiteUrl, websiteUrl.count > 0, let url = URL(string: websiteUrl), UIApplication.shared.canOpenURL(url) {
                        Button(action: {UIApplication.shared.open(url)}) {
                            Image("ic_website_circle")
                        }
                    }
                    
                    if let blogUrl = data.blogUrl, blogUrl.count > 0, let url = URL(string: blogUrl), UIApplication.shared.canOpenURL(url) {
                        Button(action: {UIApplication.shared.open(url)}) {
                            Image("ic_blog_circle")
                        }
                    }
                    
                    if let instagramUrl = data.instagramUrl, instagramUrl.count > 0, let url = URL(string: instagramUrl), UIApplication.shared.canOpenURL(url) {
                        Button(action: {UIApplication.shared.open(url)}) {
                            Image("ic_instagram_circle")
                        }
                    }
                    
                    if let youtubeUrl = data.youtubeUrl, youtubeUrl.count > 0, let url = URL(string: youtubeUrl), UIApplication.shared.canOpenURL(url) {
                        Button(action: {UIApplication.shared.open(url)}) {
                            Image("ic_youtube_circle")
                        }
                    }
                }
            }
        }
        .padding(20)
        .background(Color(hex: "222222"))
        .cornerRadius(16.7)
    }
}

struct MyInfoCardView_Previews: PreviewProvider {
    static var previews: some View {
        MyInfoCardView(
            data: MyPageResponse(
                nickname: "완다 막시모프",
                profileUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                chargeCan: 0,
                rewardCan: 150,
                youtubeUrl: "",
                instagramUrl: "",
                websiteUrl: "",
                blogUrl: "",
                liveReservationCount: 0,
                isAuth: false
            ),
            refresh: {}
        )
    }
}