# -*- coding: utf-8 -*-
"""
Created on Sun Dec  6 14:47:32 2020

@author: user
"""

import random as rd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
rd.seed(2022)



class Node(object):
    def __init__(self, nid, x, y):
        self.nid = nid
        self.x = x
        self.y = y
        self.flag = None
        self.distance = None
    def __str__(self):
        return f"nid={self.nid}, x={self.x}, y={self.y}, flag={self.flag}"
    __repr__=__str__
    

class Graph(object):
    def __init__(self, depots:list):
        
        self.depots = depots
        
        self.nodes = [Node(i, rd.randint(0,100), rd.randint(0,100)) for i in range(50)]

        for node in self.nodes:
            toDepot = [self.__length(node, d) for d in self.depots]
            flag = 0
            if toDepot[1]<toDepot[flag]:
                flag = 1
            if toDepot[2]<toDepot[flag]:
                flag = 2
            node.flag = flag
            node.distance = toDepot[flag]
            
        with open("FLPnodes.txt", 'w') as f:
            for n in self.nodes:
                f.write(f"{n}\n")
        
        self.gPlot()
    
    def __length(self, na, nb):
        return ((na.x-nb.x)**2+(na.y-nb.y)**2)**0.5
    

    def gPlot(self):
        """
        * To plot the routes 
        """
        # rid = ["Truck"]
#        idcount = 0
        linestyles = ['-','--','-.',':']
#        markers = ['o','^','s','.','v','*','p','1','<','>','x','X','d','D','+']
        plt.figure(figsize = (12,12))
        # plt.title(f"Objective={objective}\nTotal cost={totalCost}\nMax Route={maxRoute}\nMax Min Difference={difference}")
        totalDistance = 0
        for n in self.nodes:
            totalDistance += n.distance
            
        plt.title(f"Total Distance = {totalDistance}")
        plt.grid(True)
        plt.minorticks_on()
        
        nx1 = []
        ny1 = []
        nx2 = []
        ny2 = []
        nx3 = []
        ny3 = []
        
        for n in self.nodes:
            if n.flag==0:
                nx1.append(n.x)
                ny1.append(n.y)
                nx1.append(self.depots[0].x)
                ny1.append(self.depots[0].y)
            elif n.flag==1:
                nx2.append(n.x)
                ny2.append(n.y)
                nx2.append(self.depots[1].x)
                ny2.append(self.depots[1].y)    
            else:
                nx3.append(n.x)
                ny3.append(n.y)
                nx3.append(self.depots[2].x)
                ny3.append(self.depots[2].y)
            
            
        plt.plot(nx1, ny1, nx2, ny2, nx3, ny3)
        
        
        
        for n in self.nodes:
            plt.plot(n.x, n.y, "go")
            plt.text(n.x + 0.5, n.y + 0.5, f"{n.nid}")
        
        for n in self.depots:
            plt.plot(n.x, n.y, "rs")
            plt.text(n.x + 0.5, n.y + 0.5, f"{n.nid}")
        
        
        



if __name__=="__main__":
    
    
    ## 此題目為continuous Facility Location Problem
    ## 選擇三個位置做為站點
    ## 程式會自動將客戶分配至最近的站點
    ## 你的目標是找到三個站點位置，使得所有客戶到最近站點的總距離和最小
    
    
    ## 修改以下三個點的x,y座標
    x1 = 30
    y1 = 30
    x2 = 50
    y2 = 50
    x3 = 70
    y3 = 70
    ########################
    
    
    d1 = Node("ONE", x1, y1)
    d2 = Node("TWO", x2, y2)
    d3 = Node("THREE", x3, y3)
    depots = [d1, d2, d3]

    g = Graph(depots)
    
    






