//
//  UserProfileCreatorView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/11.
//

import SwiftUI
import Kingfisher

struct UserProfileCreatorView: View {
    
    let creator: CreatorResponse
    
    let creatorFollow: () -> Void
    let showCreatorFollowNotifyDialog: () -> Void
    let shareChannel: () -> Void
    
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            HStack(spacing: 20) {
                KFImage(URL(string: creator.profileUrl))
                    .cancelOnDisappear(true)
                    .downsampling(
                        size: CGSize(
                            width: 90,
                            height: 90
                        )
                    )
                    .resizable()
                    .scaledToFill()
                    .frame(width: 90, height: 90)
                    .background(Color(hex: "3e3358"))
                    .clipShape(Circle())
                
                VStack(alignment: .leading, spacing: 0) {
                    HStack(spacing: 0) {
                        Text(creator.nickname)
                            .font(.custom(Font.bold.rawValue, size: 20))
                            .foregroundColor(Color(hex: "eeeeee"))
                            .padding(.top, 6.7)
                        
                        Spacer()
                        
                        Image("btn_big_share")
                            .resizable()
                            .frame(width: 33.3, height: 33.3)
                            .onTapGesture { shareChannel() }
                    }
                    
                    if creator.creatorId == UserDefaults.int(forKey: .userId) {
                        Text("팔로워 리스트")
                            .font(.custom(Font.bold.rawValue, size: 12))
                            .foregroundColor(Color.white)
                            .padding(.vertical, 8)
                            .padding(.horizontal, 16)
                            .overlay(
                                RoundedRectangle(cornerRadius: 16.7)
                                    .stroke(Color.button, lineWidth: 1)
                            )
                            .padding(.top, 13.3)
                            .onTapGesture {
                                AppState.shared.setAppStep(step: .followerList(userId: creator.creatorId))
                            }
                    } else {
                        VStack(alignment: .leading, spacing: 9.3) {
                            Image(
                                creator.isFollow ?
                                creator.isNotify ? "btn_following_big": "btn_following_no_alarm_big"
                                : "btn_follow_big"
                            )
                                .resizable()
                                .frame(width: 83.3, height: 26.7)
                                .onTapGesture {
                                    if creator.isFollow {
                                        showCreatorFollowNotifyDialog()
                                    } else {
                                        creatorFollow()
                                    }
                                }
                            
                            Text("팔로워 \(creator.notificationRecipientCount)명")
                                .font(.custom(Font.light.rawValue, size: 12))
                                .foregroundColor(Color(hex: "bbbbbb"))
                        }
                        .padding(.top, 13.3)
                    }
                }
            }
            
            Text(creator.tags.map { "#\($0)" }.joined(separator: " "))
                .font(.custom(Font.medium.rawValue, size: 13.3))
                .foregroundColor(Color.button)
                .padding(.top, creator.tags.count > 0 ? 13.3 : 0)
            
            HStack(spacing: 10) {
                Spacer()
                
                if let websiteUrl = creator.websiteUrl, websiteUrl.count > 0, let url = URL(string: websiteUrl), UIApplication.shared.canOpenURL(url) {
                    Button(action: {UIApplication.shared.open(url)}) {
                        Image("ic_website_circle")
                            .resizable()
                            .frame(width: 40, height: 40)
                    }
                    .padding(.top, 13.3)
                }
                
                if let blogUrl = creator.blogUrl, blogUrl.count > 0, let url = URL(string: blogUrl), UIApplication.shared.canOpenURL(url) {
                    Button(action: {UIApplication.shared.open(url)}) {
                        Image("ic_blog_circle")
                            .resizable()
                            .frame(width: 40, height: 40)
                    }
                    .padding(.top, 13.3)
                }
                
                if let instagramUrl = creator.instagramUrl, instagramUrl.count > 0, let url = URL(string: instagramUrl), UIApplication.shared.canOpenURL(url) {
                    Button(action: {UIApplication.shared.open(url)}) {
                        Image("ic_instagram_circle")
                            .resizable()
                            .frame(width: 40, height: 40)
                    }
                    .padding(.top, 13.3)
                }
                
                if let youtubeUrl = creator.youtubeUrl, youtubeUrl.count > 0, let url = URL(string: youtubeUrl), UIApplication.shared.canOpenURL(url) {
                    Button(action: {UIApplication.shared.open(url)}) {
                        Image("ic_youtube_circle")
                            .resizable()
                            .frame(width: 40, height: 40)
                    }
                    .padding(.top, 13.3)
                }
            }
        }
        .padding(20)
        .background(Color(hex: "222222"))
        .cornerRadius(16.7)
        .padding(.top, 20)
        .padding(.horizontal, 13.3)
    }
}

struct UserProfileCreatorView_Previews: PreviewProvider {
    static var previews: some View {
        UserProfileCreatorView(
            creator: CreatorResponse(
                creatorId: 2,
                profileUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                nickname: "수다친구1",
                tags: ["썸", "연애", "부부"],
                introduce: "상담사1 입니다.yyyyyyy\n\n\n\n\n\n\njgdgjdgjdgicyifyicyi\n\n\n\n\n\n\n\n\n\n\n\n",
                instagramUrl: Optional("3x2tfZnfLRo"),
                youtubeUrl: Optional("https://www.youtube.com/watch?v=3x2tfZnfLRo"),
                websiteUrl: Optional("https://instagram.com/dear.zia"),
                blogUrl: Optional("dear.zia"),
                isFollow: false,
                isNotify: false,
                isNotification: false,
                notificationRecipientCount: 2
            )
        ) {
        } showCreatorFollowNotifyDialog: {
        } shareChannel: {
        }
    }
}