package com.vpnpanel;

import android.content.Context;
import android.content.Intent;
import android.net.VpnService;
import android.os.Handler;
import android.os.Looper;

import com.v2ray.ang.V2RayVPNService;
import com.v2ray.ang.util.V2RayConfig;

/**
 * مدیریت اتصال VPN با استفاده از V2Ray
 */
public class VPNManager {
    
    private Context context;
    private V2RayVPNService vpnService;
    private VPNCallback callback;
    private Handler handler;
    
    public interface VPNCallback {
        void onConnected();
        void onDisconnected();
        void onError(String error);
    }
    
    public VPNManager(Context context) {
        this.context = context;
        this.handler = new Handler(Looper.getMainLooper());
    }
    
    /**
     * اتصال به VPN
     */
    public void connect(String config, VPNCallback callback) {
        this.callback = callback;
        
        try {
            // درخواست مجوز VPN از کاربر
            Intent intent = VpnService.prepare(context);
            if (intent != null) {
                // نیاز به مجوز
                if (context instanceof MainActivity) {
                    ((MainActivity) context).startActivityForResult(intent, 100);
                }
                return;
            }
            
            // شروع سرویس VPN
            startVPNService(config);
            
        } catch (Exception e) {
            if (callback != null) {
                callback.onError("خطا در اتصال: " + e.getMessage());
            }
        }
    }
    
    private void startVPNService(String config) {
        try {
            // پارس کانفیگ V2Ray
            V2RayConfig v2rayConfig = V2RayConfig.parseConfig(config);
            
            if (v2rayConfig == null) {
                if (callback != null) {
                    callback.onError("کانفیگ نامعتبر است");
                }
                return;
            }
            
            // شروع سرویس
            Intent serviceIntent = new Intent(context, V2RayVPNService.class);
            serviceIntent.putExtra("config", config);
            context.startService(serviceIntent);
            
            // بررسی وضعیت اتصال
            checkConnectionStatus();
            
        } catch (Exception e) {
            if (callback != null) {
                callback.onError("خطا در راه‌اندازی: " + e.getMessage());
            }
        }
    }
    
    private void checkConnectionStatus() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isConnected()) {
                    if (callback != null) {
                        callback.onConnected();
                    }
                } else {
                    // بررسی مجدد
                    handler.postDelayed(this, 1000);
                }
            }
        }, 1000);
    }
    
    /**
     * قطع اتصال VPN
     */
    public void disconnect() {
        try {
            Intent serviceIntent = new Intent(context, V2RayVPNService.class);
            context.stopService(serviceIntent);
            
            if (callback != null) {
                callback.onDisconnected();
            }
        } catch (Exception e) {
            if (callback != null) {
                callback.onError("خطا در قطع اتصال: " + e.getMessage());
            }
        }
    }
    
    /**
     * بررسی وضعیت اتصال
     */
    public boolean isConnected() {
        // بررسی وضعیت سرویس V2Ray
        try {
            // این متد باید وضعیت واقعی سرویس را برگرداند
            return V2RayVPNService.isRunning();
        } catch (Exception e) {
            return false;
        }
    }
    
    /**
     * تمیز کردن منابع
     */
    public void cleanup() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}
